30 lines
1.0 KiB
C#
30 lines
1.0 KiB
C#
using UnityEngine;
|
|
|
|
public class Unit : MonoBehaviour {
|
|
private const float moveSpeed = 4f;
|
|
private const float rotationSpeed = 10f;
|
|
private const float stoppingDistance = 0.1f;
|
|
private static readonly int isWalking = Animator.StringToHash("IsWalking");
|
|
|
|
[SerializeField] private Animator UnitAnimator;
|
|
|
|
private Vector3 targetPosition;
|
|
|
|
private void Awake() => targetPosition = transform.position;
|
|
|
|
private void Update() {
|
|
if (Vector3.Distance(targetPosition, transform.position) > stoppingDistance) {
|
|
Vector3 moveDirection = (targetPosition - transform.position).normalized;
|
|
transform.position += moveSpeed * Time.deltaTime * moveDirection;
|
|
transform.forward = Vector3.Lerp(transform.forward, moveDirection, rotationSpeed * Time.deltaTime);
|
|
UnitAnimator.SetBool(isWalking, true);
|
|
}
|
|
else {
|
|
UnitAnimator.SetBool(isWalking, false);
|
|
}
|
|
}
|
|
|
|
public void Move(Vector3 newTargetPosition) {
|
|
targetPosition = newTargetPosition;
|
|
}
|
|
} |