Guest User

Untitled

a guest
May 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. public class EnemyAI : MonoBehaviour
  2. {
  3. public UnityEngine.AI.NavMeshAgent agent { get; private set; } // the navmesh agent required for the path finding
  4. public ThirdPersonCharacter character { get; private set; } // the character we are controlling
  5. public Transform target; // target to aim for
  6. public float dangerRadius = 5f;
  7.  
  8. private void Start()
  9. {
  10. // get the components on the object we need ( should not be null due to require component so no need to check )
  11. agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
  12. character = GetComponent<ThirdPersonCharacter>();
  13.  
  14. agent.updateRotation = false;
  15. agent.updatePosition = true;
  16. }
  17.  
  18.  
  19. private void Update()
  20. {
  21. if (target != null)
  22. agent.SetDestination(target.position);
  23.  
  24. if (agent.remainingDistance < dangerRadius)
  25. {
  26. agent.isStopped = false;
  27. character.Move(agent.desiredVelocity, false, false);
  28. }
  29.  
  30. else
  31. {
  32. character.Move(Vector3.zero, false, false);
  33. agent.isStopped = true;
  34. }
  35.  
  36. if (agent.remainingDistance < 1f)
  37. {
  38. //maybe call attack animation, or scream animation
  39. Debug.Log("you died bro");
  40. }
  41. }
  42.  
  43.  
  44. public void SetTarget(Transform target)
  45. {
  46. this.target = target;
  47. }
  48. }
  49. }
Add Comment
Please, Sign In to add comment