using UnityEngine;

public class chanController : MonoBehaviour
{
    public float speed = 5.0f;
    private Animator animator;
    private Camera camera;
    private bool isMove;
    private Vector3 destination;

    private void Awake()
    {
        camera = Camera.main;
    }

    private void Start()
    {
        animator = GetComponent<Animator>();
    }

    private void Update()
    {
        if (Input.GetMouseButton(1))
        {
            RaycastHit hit;
            if (Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit))
            {
                SetDestination(hit.point);
            }
        }

        Move();
    }

    private void SetDestination(Vector3 dest)
    {
        destination = dest;
        isMove = true;
        animator.SetInteger("state", 1); // 목적지 설정 시 애니메이션 상태를 "run"으로 설정
    }

    private void Move()
    {
        if (isMove)
        {
            if (Vector3.Distance(destination, transform.position) <= 0.1f)
            {
                isMove = false;
                animator.SetInteger("state", 0); // 목적지에 도달했을 때 애니메이션 상태를 "idle"로 설정
                return;
            }

            Vector3 dir = (destination - transform.position).normalized;
            transform.forward = dir;
            transform.position += dir * Time.deltaTime * speed;
        }
    }
}

+ Recent posts