From fae1548cf4ced2ad60aefda5ceaaa86205780b6e Mon Sep 17 00:00:00 2001 From: nothke Date: Sat, 17 Aug 2024 16:39:05 +0200 Subject: [PATCH] More realistic magnets and a little bit of waiting time --- Assets/Scripts/CranePickDrop.cs | 19 +++++++++++++++++-- Assets/Scripts/Magnet.cs | 7 ++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/CranePickDrop.cs b/Assets/Scripts/CranePickDrop.cs index ae66884..c5569dd 100644 --- a/Assets/Scripts/CranePickDrop.cs +++ b/Assets/Scripts/CranePickDrop.cs @@ -16,12 +16,15 @@ public class CranePickDrop : MonoBehaviour { Idle, Catching, + WaitingToCatch, Tansporting, Finished, } public State state; + float timer = 0; + public void OnTriggerEnterSignalReceived(EnterTriggerSender sender) { var otherRb = sender.triggeredCollider.attachedRigidbody; @@ -29,6 +32,7 @@ public class CranePickDrop : MonoBehaviour if (otherRb && otherRb.isKinematic == false && state == State.Idle) { handlingBody = otherRb; + crane.testTgt = handlingBody.transform; state = State.Catching; } } @@ -51,11 +55,22 @@ public class CranePickDrop : MonoBehaviour magnet.strength = magnetStrength; - crane.testTgt = handlingBody.transform; if (magnet.IsCloseTo(handlingBody, 2f)) { - crane.testTgt = dropTarget; + crane.testTgt = null; + + state = State.WaitingToCatch; + timer = 3; + } + } + else if (state == State.WaitingToCatch) + { + timer -= Time.deltaTime; + + if (timer < 0) + { state = State.Tansporting; + crane.testTgt = dropTarget; } } diff --git a/Assets/Scripts/Magnet.cs b/Assets/Scripts/Magnet.cs index 770fbfe..ac964cd 100644 --- a/Assets/Scripts/Magnet.cs +++ b/Assets/Scripts/Magnet.cs @@ -17,12 +17,13 @@ public class Magnet : MonoBehaviour { Vector3 dir = otherRb.position - transform.position; float dirSq = Vector3.SqrMagnitude(dir); - float force = strength * (1.0f / dirSq); + float forceMagnitude = strength * (1.0f / dirSq); + Vector3 force = dir.normalized * forceMagnitude; - otherRb.AddForce(-dir * force); + otherRb.AddForce(-force); if (rb) - rb.AddForce(dir * force); + rb.AddForce(force); } } }