From 6ce8b9e22fc13be34b442c7b6af48b42cd44275a Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Wed, 13 Mar 2024 11:00:58 +0800 Subject: +init --- SetInertiaTension.cs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 SetInertiaTension.cs (limited to 'SetInertiaTension.cs') 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(); + holdable = GetComponent(); + 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; + } + } +} -- cgit v1.1-26-g67d0