Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(NavMeshAgent))]
  4. [RequireComponent(typeof(ThirdPersonCharacter))]
  5. public class AICharacterControl : MonoBehaviour {
  6.  
  7. public NavMeshAgent agent { get; private set; } // the navmesh agent required for the path finding
  8. public ThirdPersonCharacter character { get; private set; } // the character we are controlling
  9. public Transform target; // target to aim for
  10. public float targetChangeTolerance = 1f; // distance to target before target can be changed
  11.  
  12. Vector3 targetPos;
  13.  
  14. // Use this for initialization
  15. void Start () {
  16.  
  17. // get the components on the object we need ( should not be null due to require component so no need to check )
  18. agent = GetComponentInChildren<NavMeshAgent>();
  19. character = GetComponent<ThirdPersonCharacter>();
  20.  
  21. }
  22.  
  23. // Update is called once per frame
  24. void Update () {
  25.  
  26. if (target != null)
  27. {
  28.  
  29. // update the progress if the character has made it to the previous target
  30. if ((target.position-targetPos).magnitude > targetChangeTolerance) {
  31. targetPos = target.position;
  32. agent.SetDestination(targetPos);
  33. }
  34.  
  35. // update the agents posiiton
  36. agent.transform.position = transform.position;
  37.  
  38. // use the values to move the character
  39. character.Move( agent.desiredVelocity, false, false, targetPos );
  40.  
  41. } else {
  42.  
  43. // We still need to call the character's move function, but we send zeroed input as the move param.
  44. character.Move ( Vector3.zero, false, false, transform.position + transform.forward * 100 );
  45.  
  46. }
  47. }
  48.  
  49. public void SetTarget(Transform target)
  50. {
  51. this.target = target;
  52. }
  53.  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement