본문 바로가기

프로그래밍/Unity

(6)
[Unity] Mouse 이벤트 (다운, 업, 클릭, 드래그) [Unity] Mouse 이벤트 (다운, 업, 클릭, 드래그) 마우스 다운, 업 이벤트 - PointerEventData로 pointerId, position 등을 알수 있다. using UnityEngine; using UnityEngine.EventSystems; public class Example : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { //Detect current clicks on the GameObject (the one with the script attached) public void OnPointerDown(PointerEventData pointerEventData) { //Output the name of the Game..
[Unity] Mouse 클릭한 지점에 객체 생성하기 (Ray 사용) [Unity] Mouse 클릭한 지점에 객체 생성하기 (Ray 사용) using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public GameObject particle; void Update() { if (Input.GetButtonDown("Fire1")) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray)) Instantiate(particle, transform.position, transform.rotation); } } }
[Unity] 버튼에 마우스 클릭 이벤트 사용하기 Unity 버튼에 마우스 클릭 이벤트 사용하기 using UnityEngine; using UnityEngine.UI; public class Example : MonoBehaviour { //Make sure to attach these Buttons in the Inspector public Button m_YourFirstButton, m_YourSecondButton, m_YourThirdButton; void Start() { //Calls the TaskOnClick/TaskWithParameters/ButtonClicked method when you click the Button m_YourFirstButton.onClick.AddListener(TaskOnClick); m_YourSeco..
[Unity] 마우스 방향으로 이동 Unity 마우스 방향으로 이동 public Camera mainCamera; //메인 카메라 public float maxSpeed; //최대 속도 public float aclrt; //가속도 private float curr_speed; //현재 속도 private void Update () { if (Input.GetMouseButton (0)) { Ray ray = mainCamera.ScreenPointToRay (Input.mousePosition); RaycastHit hit; if (Physics.Raycast (ray, out hit, Mathf.Infinity) { Vector3 dir = new Vector3 (hit.point.x - transform.position.x, 0f, ..
[Unity] 마우스 클릭 지점으로 이동 Unity 마우스 클릭 지점으로 이동 using UnityEngine; public class ClickMove : MonoBehaviour { private Ray ray; private RaycastHit hit; private Vector3 movePos = Vector3.zero; private Transform tr; private Animator anim; // Use this for initialization void Start () { tr = GetComponent(); anim = GetComponent(); } // Update is called once per frame void Update () { ray = Camera.main.ScreenPointToRay(Input.mouse..
[Unity] 캐릭터 좌우로 움직이기 (RigidBody) Unity 캐릭터 좌우로 움직이기 (RigidBody) [Header("속도 관련 변수")][Range(0,1)] [SerializeField] float moveSpeed; //강제로 인스펙터 창에 띄울 수 있음 Rigidbody myRigid; // Update is called once per frame void Update () { if (Input.GetAxisRaw("Horizontal") != 0) // 왼쪽 -1, 오른쪽 1 { Vector3 moveDir = new Vector3(0, 0, Input.GetAxisRaw("Horizontal")); myRigid.AddForce(moveDir * moveSpeed); } }