summaryrefslogtreecommitdiff
path: root/Thronefall_1_0/Decompile/KeepRelativeRotationTo.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-05-19 16:46:27 +0800
committerchai <215380520@qq.com>2024-05-19 16:46:27 +0800
commit8b1fc7063b387542803c6bc214ccf8acb32870bd (patch)
treed310eb99872c8215f1c1f67731ec21f0915cd778 /Thronefall_1_0/Decompile/KeepRelativeRotationTo.cs
parent8e13e7e2874adc8982e16d1d2ed2e28d7480b45f (diff)
* rename
Diffstat (limited to 'Thronefall_1_0/Decompile/KeepRelativeRotationTo.cs')
-rw-r--r--Thronefall_1_0/Decompile/KeepRelativeRotationTo.cs59
1 files changed, 59 insertions, 0 deletions
diff --git a/Thronefall_1_0/Decompile/KeepRelativeRotationTo.cs b/Thronefall_1_0/Decompile/KeepRelativeRotationTo.cs
new file mode 100644
index 0000000..94dbe16
--- /dev/null
+++ b/Thronefall_1_0/Decompile/KeepRelativeRotationTo.cs
@@ -0,0 +1,59 @@
+using UnityEngine;
+
+public class KeepRelativeRotationTo : MonoBehaviour
+{
+ [SerializeField]
+ private Transform transformToKeepRelativeRotationTo;
+
+ [SerializeField]
+ private Vector3 upVector = Vector3.up;
+
+ private Quaternion initialOffset;
+
+ private Vector3 selfPosition;
+
+ private Vector3 otherPosition;
+
+ private Quaternion localRotationStart;
+
+ private Vector3 upVectorAdjusted;
+
+ private void PrepareData()
+ {
+ if ((bool)base.transform.parent)
+ {
+ selfPosition = base.transform.localPosition;
+ otherPosition = base.transform.parent.worldToLocalMatrix.MultiplyPoint(transformToKeepRelativeRotationTo.position);
+ upVectorAdjusted = upVector;
+ }
+ else
+ {
+ selfPosition = base.transform.position;
+ otherPosition = transformToKeepRelativeRotationTo.position;
+ upVectorAdjusted = upVector;
+ }
+ }
+
+ private void Start()
+ {
+ if ((bool)base.transform.parent)
+ {
+ upVector = base.transform.localToWorldMatrix.MultiplyVector(upVector);
+ upVector = base.transform.parent.localToWorldMatrix.MultiplyVector(upVector);
+ upVector = upVector.normalized;
+ }
+ localRotationStart = base.transform.localRotation;
+ PrepareData();
+ initialOffset = Quaternion.Inverse(Quaternion.LookRotation(otherPosition - selfPosition, upVectorAdjusted));
+ }
+
+ private void Update()
+ {
+ if ((bool)transformToKeepRelativeRotationTo)
+ {
+ PrepareData();
+ Quaternion quaternion = Quaternion.LookRotation(otherPosition - selfPosition, upVectorAdjusted);
+ base.transform.localRotation = quaternion * initialOffset * localRotationStart;
+ }
+ }
+}