AstarSmoothFollow2.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using UnityEngine;
  2. namespace Pathfinding.Examples {
  3. /// <summary>
  4. /// Smooth Camera Following.
  5. /// \author http://wiki.unity3d.com/index.php/SmoothFollow2
  6. /// </summary>
  7. [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_astar_smooth_follow2.php")]
  8. public class AstarSmoothFollow2 : MonoBehaviour {
  9. public Transform target;
  10. public float distance = 3.0f;
  11. public float height = 3.0f;
  12. public float damping = 5.0f;
  13. public bool smoothRotation = true;
  14. public bool followBehind = true;
  15. public float rotationDamping = 10.0f;
  16. public bool staticOffset = false;
  17. void LateUpdate () {
  18. Vector3 wantedPosition;
  19. if (staticOffset) {
  20. wantedPosition = target.position + new Vector3(0, height, distance);
  21. } else {
  22. if (followBehind)
  23. wantedPosition = target.TransformPoint(0, height, -distance);
  24. else
  25. wantedPosition = target.TransformPoint(0, height, distance);
  26. }
  27. transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
  28. if (smoothRotation) {
  29. Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
  30. transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
  31. } else transform.LookAt(target, target.up);
  32. }
  33. }
  34. }