More realistic magnets and a little bit of waiting time

This commit is contained in:
nothke 2024-08-17 16:39:05 +02:00
parent 41ed2e559f
commit fae1548cf4
2 changed files with 21 additions and 5 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}
}