2024-08-17 14:13:26 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class CranePickDrop : MonoBehaviour
|
|
|
|
{
|
|
|
|
public SlidingCrane crane;
|
|
|
|
public Magnet magnet;
|
|
|
|
public Transform dropTarget;
|
|
|
|
|
|
|
|
public Rigidbody handlingBody;
|
|
|
|
|
|
|
|
public float magnetStrength;
|
|
|
|
|
|
|
|
public enum State
|
|
|
|
{
|
|
|
|
Idle,
|
|
|
|
Catching,
|
2024-08-17 14:39:05 +00:00
|
|
|
WaitingToCatch,
|
2024-08-17 14:13:26 +00:00
|
|
|
Tansporting,
|
|
|
|
Finished,
|
|
|
|
}
|
|
|
|
|
|
|
|
public State state;
|
|
|
|
|
2024-08-17 14:39:05 +00:00
|
|
|
float timer = 0;
|
|
|
|
|
2024-08-17 14:13:26 +00:00
|
|
|
public void OnTriggerEnterSignalReceived(EnterTriggerSender sender)
|
|
|
|
{
|
|
|
|
var otherRb = sender.triggeredCollider.attachedRigidbody;
|
|
|
|
|
|
|
|
if (otherRb && otherRb.isKinematic == false && state == State.Idle)
|
|
|
|
{
|
|
|
|
handlingBody = otherRb;
|
2024-08-17 14:39:05 +00:00
|
|
|
crane.testTgt = handlingBody.transform;
|
2024-08-17 14:13:26 +00:00
|
|
|
state = State.Catching;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnTriggerExitSignalReceived(EnterTriggerSender sender)
|
|
|
|
{
|
|
|
|
var otherRb = sender.triggeredCollider;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
magnetStrength = magnet.strength;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
if (state == State.Catching)
|
|
|
|
{
|
|
|
|
Debug.Assert(handlingBody, this);
|
|
|
|
|
|
|
|
magnet.strength = magnetStrength;
|
|
|
|
|
2024-08-17 14:29:47 +00:00
|
|
|
if (magnet.IsCloseTo(handlingBody, 2f))
|
2024-08-17 14:13:26 +00:00
|
|
|
{
|
2024-08-17 14:39:05 +00:00
|
|
|
crane.testTgt = null;
|
|
|
|
|
|
|
|
state = State.WaitingToCatch;
|
|
|
|
timer = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (state == State.WaitingToCatch)
|
|
|
|
{
|
|
|
|
timer -= Time.deltaTime;
|
|
|
|
|
|
|
|
if (timer < 0)
|
|
|
|
{
|
2024-08-17 14:13:26 +00:00
|
|
|
state = State.Tansporting;
|
2024-08-17 14:39:05 +00:00
|
|
|
crane.testTgt = dropTarget;
|
2024-08-17 14:13:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (state == State.Tansporting)
|
|
|
|
{
|
|
|
|
if (magnet.IsCloseToPlanar(dropTarget))
|
|
|
|
{
|
|
|
|
magnet.strength = 0;
|
|
|
|
state = State.Idle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|