summaryrefslogtreecommitdiffhomepage
path: root/Assets/Scripts/RigidBodyCustomGravity.cs
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2022-12-11 02:54:14 -0500
committerrealtradam <[email protected]>2022-12-11 02:54:14 -0500
commit4e32bf914903b88a766e7d6166faf34cb4ba4620 (patch)
tree7479dc98bdd6c90433b3286b188d1e9b861bad78 /Assets/Scripts/RigidBodyCustomGravity.cs
parent1b7c9c7fc27541bb9420b91b3cd68f3a726e435c (diff)
downloadMagnet-Run-3D-4e32bf914903b88a766e7d6166faf34cb4ba4620.tar.gz
Magnet-Run-3D-4e32bf914903b88a766e7d6166faf34cb4ba4620.zip
single planet gravity
Diffstat (limited to 'Assets/Scripts/RigidBodyCustomGravity.cs')
-rw-r--r--Assets/Scripts/RigidBodyCustomGravity.cs59
1 files changed, 59 insertions, 0 deletions
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<Rigidbody>();
+ renderer = GetComponent<Renderer>();
+ 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
+ );
+ }
+}