| | | 1 | | using UnityEngine; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Match the rotation of the target transform |
| | | 5 | | /// </summary> |
| | | 6 | | public class MatchRotation : MonoBehaviour |
| | | 7 | | { |
| | | 8 | | [Tooltip("Match x rotation")] |
| | 5 | 9 | | public bool matchX = false; |
| | | 10 | | |
| | | 11 | | [Tooltip("Match y rotation")] |
| | 5 | 12 | | public bool matchY = false; |
| | | 13 | | |
| | | 14 | | [Tooltip("Match z rotation")] |
| | 5 | 15 | | public bool matchZ = false; |
| | | 16 | | |
| | | 17 | | [Tooltip("The transform this object will match")] |
| | 5 | 18 | | public Transform targetTransform = null; |
| | 5 | 19 | | private Vector3 originalRotation = Vector3.zero; |
| | | 20 | | |
| | | 21 | | private void Awake() |
| | 2 | 22 | | { |
| | 2 | 23 | | originalRotation = transform.eulerAngles; |
| | 2 | 24 | | } |
| | | 25 | | |
| | | 26 | | public void FollowRotation() |
| | 2 | 27 | | { |
| | 2 | 28 | | Vector3 newRotation = targetTransform.eulerAngles; |
| | | 29 | | |
| | 2 | 30 | | newRotation.x = matchX ? newRotation.x : originalRotation.x; |
| | 2 | 31 | | newRotation.y = matchY ? newRotation.y : originalRotation.y; |
| | 2 | 32 | | newRotation.z = matchZ ? newRotation.z : originalRotation.z; |
| | | 33 | | |
| | 2 | 34 | | transform.rotation = Quaternion.Euler(newRotation); |
| | 2 | 35 | | } |
| | | 36 | | } |