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);
}
}
}