Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Pathfinding;
  5.  
  6. public class MonsterFollow : MonoBehaviour
  7. {
  8. // The point to move to
  9. public Transform targetPosition;
  10. private Seeker seeker;
  11. private CharacterController controller;
  12. // The calculated path
  13. public Path path;
  14. // The AI's speed in meters per second
  15. public float speed = 2;
  16. // The max distance from the AI to a waypoint for it to continue to the next waypoint
  17. public float nextWaypointDistance = 3;
  18. // The waypoint we are currently moving towards
  19. private int currentWaypoint = 0;
  20. // How often to recalculate the path (in seconds)
  21. public float repathRate = 0.5f;
  22. private float lastRepath = -9999;
  23.  
  24. // Use this for initialization
  25. void Start ()
  26. {
  27. seeker = GetComponent<Seeker>();
  28. controller = GetComponent<CharacterController>();
  29.  
  30. //Start a new path to the targetPosition, return the result to the OnPathComplete function
  31. //seeker.StartPath(transform.position, targetPosition, OnPathComplete);
  32. }
  33.  
  34. // Update is called once per frame
  35. void Update ()
  36. {
  37. if (Time.time - lastRepath > repathRate && seeker.IsDone())
  38. {
  39. lastRepath = Time.time + UnityEngine.Random.value * repathRate * 0.5f;
  40. // Start a new path to the targetPosition, call the the OnPathComplete function
  41. // when the path has been calculated (which may take a few frames depending on the complexity)
  42. seeker.StartPath(transform.position, targetPosition.position, OnPathComplete);
  43. }
  44. if (path == null)
  45. {
  46. // We have no path to follow yet, so don't do anything
  47. return;
  48. }
  49. if (currentWaypoint > path.vectorPath.Count) return;
  50. if (currentWaypoint == path.vectorPath.Count)
  51. {
  52. Debug.Log("End Of Path Reached");
  53. currentWaypoint++;
  54. return;
  55. }
  56. // Direction to the next waypoint
  57. Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
  58. dir *= speed;
  59. // Note that SimpleMove takes a velocity in meters/second, so we should not multiply by Time.deltaTime
  60. controller.SimpleMove(dir);
  61. // The commented line is equivalent to the one below, but the one that is used
  62. // is slightly faster since it does not have to calculate a square root
  63. //if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
  64. if ((transform.position - path.vectorPath[currentWaypoint]).sqrMagnitude < nextWaypointDistance * nextWaypointDistance)
  65. {
  66. currentWaypoint++;
  67. return;
  68. }
  69. }
  70.  
  71. public void OnPathComplete(Path p)
  72. {
  73. Debug.Log("A path was calculated. Did it fail with an error? " + p.error);
  74. if (!p.error)
  75. {
  76. path = p;
  77. // Reset the waypoint counter so that we start to move towards the first point in the path
  78. currentWaypoint = 0;
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement