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;
}
}
}
'KDT > 유니티 기초' 카테고리의 다른 글
[Apple Catch] Apple Catch 제작 (0) | 2023.08.07 |
---|---|
[MoveHunt] 몬스터를 클릭하면 이동 후 공격, 바닥을 클릭하면 해당 위치로 이동 구현 (1) | 2023.08.06 |
[밤 송이 던지기] (0) | 2023.08.04 |
[Pixel Sword] 캐릭터 동작과 애니메이션 구현 + 캐릭터 위의 땅 충돌시 오류 수정 (0) | 2023.08.04 |
[ClimbCloud] 제작 (0) | 2023.08.02 |