Files
2026-02-21 16:45:37 +08:00

43 lines
896 B
C#

using UnityEngine;
using UnityEngine.AI;
namespace UnityStandardAssets.Characters.ThirdPerson
{
[RequireComponent(typeof(NavMeshAgent))]
[RequireComponent(typeof(ThirdPersonCharacter))]
public class AICharacterControl : MonoBehaviour
{
public Transform target;
public NavMeshAgent agent { get; private set; }
public ThirdPersonCharacter character { get; private set; }
private void Start()
{
agent = GetComponentInChildren<NavMeshAgent>();
character = GetComponent<ThirdPersonCharacter>();
agent.updateRotation = false;
agent.updatePosition = true;
}
private void Update()
{
if (target != null)
{
agent.SetDestination(target.position);
character.Move(agent.desiredVelocity, false, false);
}
else
{
character.Move(Vector3.zero, false, false);
}
}
public void SetTarget(Transform target)
{
this.target = target;
}
}
}