[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<Transform>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 100.0f, Color.green);
if (Input.GetMouseButtonDown(0) && Physics.Raycast(ray, out hit, 100.0f, 1<<8))
{
movePos = hit.point;
Debug.Log(movePos);
}
if ((tr.position - movePos).sqrMagnitude >= 0.2f * 0.2f)
{
Quaternion rot = Quaternion.LookRotation(movePos - tr.position);
tr.rotation = Quaternion.Slerp(tr.rotation, rot, Time.deltaTime * 8.0f);
tr.Translate(Vector3.forward * Time.deltaTime * 3.0f);
anim.SetBool("isRun", true);
}
else
{
anim.SetBool("isRun", false);
}
}
}
'Unity > Input' 카테고리의 다른 글
[Unity] Debug.Log를 화면에 보여주기 (GUILayout) (0) | 2020.10.23 |
---|---|
[Unity] 버튼에 마우스 클릭 이벤트 사용하기 (0) | 2020.09.21 |
[Unity] 캐릭터 좌우로 움직이기 (RigidBody) (0) | 2020.09.12 |