FleePath.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using UnityEngine;
  2. namespace Pathfinding {
  3. /// <summary>
  4. /// Returns a path heading away from a specified point to avoid.
  5. /// The search will terminate when G \> length (passed to the constructor) + FleePath.spread.
  6. ///
  7. /// Can be used to make an AI to flee from an enemy (cannot guarantee that it will not be forced into corners though :D )
  8. /// <code>
  9. ///
  10. /// // Call a FleePath call like this, assumes that a Seeker is attached to the GameObject
  11. /// Vector3 thePointToFleeFrom = Vector3.zero;
  12. ///
  13. /// // The path will be returned when the path is over a specified length (or more accurately when the traversal cost is greater than a specified value).
  14. /// // A score of 1000 is approximately equal to the cost of moving one world unit.
  15. /// int theGScoreToStopAt = 10000;
  16. ///
  17. /// // Create a path object
  18. /// FleePath path = FleePath.Construct (transform.position, thePointToFleeFrom, theGScoreToStopAt);
  19. /// // This is how strongly it will try to flee, if you set it to 0 it will behave like a RandomPath
  20. /// path.aimStrength = 1;
  21. /// // Determines the variation in path length that is allowed
  22. /// path.spread = 4000;
  23. ///
  24. /// // Get the Seeker component which must be attached to this GameObject
  25. /// Seeker seeker = GetComponent<Seeker>();
  26. ///
  27. /// // Start the path and return the result to MyCompleteFunction (which is a function you have to define, the name can of course be changed)
  28. /// seeker.StartPath(path, MyCompleteFunction);
  29. ///
  30. /// </code>
  31. /// </summary>
  32. public class FleePath : RandomPath {
  33. /// <summary>
  34. /// Default constructor.
  35. /// Do not use this. Instead use the static Construct method which can handle path pooling.
  36. /// </summary>
  37. public FleePath () {}
  38. /// <summary>
  39. /// Constructs a new FleePath.
  40. /// The FleePath will be taken from a pool.
  41. /// </summary>
  42. public static FleePath Construct (Vector3 start, Vector3 avoid, int searchLength, OnPathDelegate callback = null) {
  43. var p = PathPool.GetPath<FleePath>();
  44. p.Setup(start, avoid, searchLength, callback);
  45. return p;
  46. }
  47. protected void Setup (Vector3 start, Vector3 avoid, int searchLength, OnPathDelegate callback) {
  48. Setup(start, searchLength, callback);
  49. // Set the aim to a point in the opposite direction from the point we to avoid
  50. // TODO: Why is this multiplication by 10 here?
  51. // Might want to remove it
  52. aim = start - (avoid-start)*10;
  53. }
  54. }
  55. }