summaryrefslogtreecommitdiff
path: root/SetInertiaTension.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-03-13 11:00:58 +0800
committerchai <215380520@qq.com>2024-03-13 11:00:58 +0800
commit6ce8b9e22fc13be34b442c7b6af48b42cd44275a (patch)
treeb38119d2acf0a982cb67e381f146924b9bfc3b3f /SetInertiaTension.cs
+init
Diffstat (limited to 'SetInertiaTension.cs')
-rw-r--r--SetInertiaTension.cs43
1 files changed, 43 insertions, 0 deletions
diff --git a/SetInertiaTension.cs b/SetInertiaTension.cs
new file mode 100644
index 0000000..82a713c
--- /dev/null
+++ b/SetInertiaTension.cs
@@ -0,0 +1,43 @@
+using UnityEngine;
+
+public class SetInertiaTension : MonoBehaviour
+{
+ private Vector3 defaultInertia;
+
+ private Vector3 stableInertia = Vector3.one;
+
+ public float collisionValue;
+
+ private Rigidbody rig;
+
+ private HoldableObject holdable;
+
+ private void Start()
+ {
+ rig = GetComponent<Rigidbody>();
+ holdable = GetComponent<HoldableObject>();
+ defaultInertia = rig.inertiaTensor;
+ }
+
+ private void Update()
+ {
+ collisionValue = Mathf.Lerp(collisionValue, 0f, Time.deltaTime * 7f);
+ rig.inertiaTensor = Vector3.Lerp(stableInertia, defaultInertia, Mathf.Clamp(collisionValue, 0f, 1f));
+ }
+
+ private void OnCollisionEnter(Collision collision)
+ {
+ if (!(collision.transform.root == base.transform.root) && !(collision.transform.root == holdable.holder))
+ {
+ collisionValue += collision.relativeVelocity.magnitude * 0.4f;
+ }
+ }
+
+ private void OnCollisionStay(Collision collision)
+ {
+ if (!(collision.transform.root == base.transform.root) && !(collision.transform.root == holdable.holder))
+ {
+ collisionValue += collision.relativeVelocity.magnitude * 0.2f;
+ }
+ }
+}