LegacyRVOController.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using UnityEngine;
  2. using Pathfinding;
  3. using System.Collections.Generic;
  4. namespace Pathfinding.Legacy {
  5. using Pathfinding.RVO;
  6. /// <summary>
  7. /// RVO Character Controller.
  8. /// Designed to be used as a drop-in replacement for the Unity Character Controller,
  9. /// it supports almost all of the same functions and fields with the exception
  10. /// that due to the nature of the RVO implementation, desired velocity is set in the Move function
  11. /// and is assumed to stay the same until something else is requested (as opposed to reset every frame).
  12. ///
  13. /// For documentation of many of the variables of this class: refer to the Pathfinding.RVO.IAgent interface.
  14. ///
  15. /// Note: Requires an RVOSimulator in the scene
  16. ///
  17. /// See: Pathfinding.RVO.IAgent
  18. /// See: RVOSimulator
  19. ///
  20. /// Deprecated: Use the RVOController class instead. This class only exists for compatibility reasons.
  21. /// </summary>
  22. [AddComponentMenu("Pathfinding/Legacy/Local Avoidance/Legacy RVO Controller")]
  23. [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_legacy_1_1_legacy_r_v_o_controller.php")]
  24. public class LegacyRVOController : RVOController {
  25. /// <summary>
  26. /// Layer mask for the ground.
  27. /// The RVOController will raycast down to check for the ground to figure out where to place the agent.
  28. /// </summary>
  29. [Tooltip("Layer mask for the ground. The RVOController will raycast down to check for the ground to figure out where to place the agent")]
  30. public new LayerMask mask = -1;
  31. public new bool enableRotation = true;
  32. public new float rotationSpeed = 30;
  33. public void Update () {
  34. if (rvoAgent == null) return;
  35. RaycastHit hit;
  36. Vector3 pos = tr.position + CalculateMovementDelta(Time.deltaTime);
  37. if (mask != 0 && Physics.Raycast(pos + Vector3.up*height*0.5f, Vector3.down, out hit, float.PositiveInfinity, mask)) {
  38. pos.y = hit.point.y;
  39. } else {
  40. pos.y = 0;
  41. }
  42. tr.position = pos + Vector3.up*(height*0.5f - center);
  43. if (enableRotation && velocity != Vector3.zero) transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(velocity), Time.deltaTime * rotationSpeed * Mathf.Min(velocity.magnitude, 0.2f));
  44. }
  45. }
  46. }