From 4e32bf914903b88a766e7d6166faf34cb4ba4620 Mon Sep 17 00:00:00 2001 From: realtradam Date: Sun, 11 Dec 2022 02:54:14 -0500 Subject: single planet gravity --- Assets/Scripts/RigidBodyCustomGravity.cs | 59 ++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Assets/Scripts/RigidBodyCustomGravity.cs (limited to 'Assets/Scripts/RigidBodyCustomGravity.cs') diff --git a/Assets/Scripts/RigidBodyCustomGravity.cs b/Assets/Scripts/RigidBodyCustomGravity.cs new file mode 100644 index 0000000..516f937 --- /dev/null +++ b/Assets/Scripts/RigidBodyCustomGravity.cs @@ -0,0 +1,59 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +[RequireComponent(typeof(Rigidbody))] +public class RigidBodyCustomGravity : MonoBehaviour +{ + Rigidbody body; + Renderer renderer; + float floatDelay = 0f; + + void Awake() + { + body = GetComponent(); + renderer = GetComponent(); + body.useGravity = false; + } + + void FixedUpdate() + { + if(body.IsSleeping()) + { + renderer.material.SetColor( + "_BaseColor", + Color.gray + ); + floatDelay = 0f; + return; + } + + if(body.velocity.sqrMagnitude < 0.0005f) + { + // disable interpolation when ready to sleep + body.interpolation = RigidbodyInterpolation.None; + renderer.material.SetColor( + "_BaseColor", + Color.yellow + ); + floatDelay += Time.deltaTime; + if(floatDelay >= 1f) + return; + } + else + floatDelay = 0f; + + // enable interpolation when not sleeping + body.interpolation = RigidbodyInterpolation.Interpolate; + + renderer.material.SetColor( + "_BaseColor", + Color.red + ); + + body.AddForce( + CustomGravity.GetGravity(body.position), + ForceMode.Acceleration + ); + } +} -- cgit v1.2.3