52 lines
1.2 KiB
C#
52 lines
1.2 KiB
C#
|
|
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<NavMeshAgent>();
|
||
|
|
animator = GetComponentInChildren<Animator>();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Update ()
|
||
|
|
{
|
||
|
|
if (moveTarget != null && Time.time - lastMoveToUpdate > moveToUpdateRate)
|
||
|
|
{
|
||
|
|
lastMoveToUpdate = Time.time;
|
||
|
|
MoveToPosition(moveTarget.position);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void LookTowards (Vector3 direction)
|
||
|
|
{
|
||
|
|
transform.rotation = Quaternion.LookRotation(direction);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void MoveToTarget (Transform target)
|
||
|
|
{
|
||
|
|
animator.SetBool("isMoving", true);
|
||
|
|
moveTarget = target;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void MoveToPosition (Vector3 position)
|
||
|
|
{
|
||
|
|
agent.SetDestination(position);
|
||
|
|
agent.isStopped = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void StopMovement ()
|
||
|
|
{
|
||
|
|
agent.isStopped = true;
|
||
|
|
moveTarget = null;
|
||
|
|
animator.SetBool("isMoving", false);
|
||
|
|
}
|
||
|
|
}
|