Turret.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion;
  4. using RootMotion.FinalIK;
  5. namespace RootMotion.Demos {
  6. /// <summary>
  7. /// Demo for aiming a Turret. All it does is call Transform.LookAt on each part and apply rotation limits one by one, starting from the parent.
  8. /// </summary>
  9. public class Turret : MonoBehaviour {
  10. /// <summary>
  11. /// An independent part of the turret
  12. /// </summary>
  13. [System.Serializable]
  14. public class Part {
  15. public Transform transform; // The Transform
  16. private RotationLimit rotationLimit; // The Rotation Limit component
  17. // Aim this part at the target
  18. public void AimAt(Transform target) {
  19. transform.LookAt(target.position, transform.up);
  20. // Finding the Rotation Limit
  21. if (rotationLimit == null) {
  22. rotationLimit = transform.GetComponent<RotationLimit>();
  23. rotationLimit.Disable();
  24. }
  25. // Apply rotation limits
  26. rotationLimit.Apply();
  27. }
  28. }
  29. public Transform target; // The aiming target
  30. public Part[] parts; // All the turret parts
  31. void Update() {
  32. // Rotate the turret parts one by one
  33. foreach (Part part in parts) part.AimAt(target);
  34. }
  35. }
  36. }