using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class CharacterController : MonoBehaviour { private float moveToUpdateRate = 0.1f; private float lastMoveToUpdate; private Transform moveTarget; private NavMeshAgent agent; private Animator animator; void Awake () { agent = GetComponent(); animator = GetComponentInChildren(); } void Update () { if (moveTarget != null && Time.time - lastMoveToUpdate > moveToUpdateRate) { animator.ResetTrigger("attack"); lastMoveToUpdate = Time.time; MoveToPosition(moveTarget.position); } if (animator != null) { float speed = agent.velocity.magnitude; float normalisedSpeed = speed / agent.speed; animator.SetFloat("moveSpeed", normalisedSpeed); } } public void LookTowards (Vector3 direction) { transform.rotation = Quaternion.LookRotation(direction); } public void MoveToTarget (Transform target) { moveTarget = target; } public void MoveToPosition (Vector3 position) { agent.SetDestination(position); agent.isStopped = false; } public void StopMovement () { agent.isStopped = true; moveTarget = null; } }