AICharacterControl.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.Characters.ThirdPerson
  4. {
  5. [RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))]
  6. [RequireComponent(typeof (ThirdPersonCharacter))]
  7. public class AICharacterControl : MonoBehaviour
  8. {
  9. public UnityEngine.AI.NavMeshAgent agent { get; private set; } // the navmesh agent required for the path finding
  10. public ThirdPersonCharacter character { get; private set; } // the character we are controlling
  11. public Transform target; // target to aim for
  12. private void Start()
  13. {
  14. // get the components on the object we need ( should not be null due to require component so no need to check )
  15. agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
  16. character = GetComponent<ThirdPersonCharacter>();
  17. agent.updateRotation = false;
  18. agent.updatePosition = true;
  19. }
  20. private void Update()
  21. {
  22. if (target != null)
  23. agent.SetDestination(target.position);
  24. if (agent.remainingDistance > agent.stoppingDistance)
  25. character.Move(agent.desiredVelocity, false, false);
  26. else
  27. character.Move(Vector3.zero, false, false);
  28. }
  29. public void SetTarget(Transform target)
  30. {
  31. this.target = target;
  32. }
  33. }
  34. }