APM/Assets/Scripting/ForkSensor.cs
2025-11-17 15:02:30 +07:00

57 lines
1.4 KiB
C#

using UnityEngine;
public class ForkSensor : MonoBehaviour
{
public Transform forkLift; // Vị trí animation nâng/hạ
public Animator animator; // Bộ điều khiển animation
public FixedJoint joint; // Joint dùng để gắn pallet
public Rigidbody currentPallet; // Pallet đang tương tác
void Update()
{
if (Input.GetKeyDown(KeyCode.U)) // Up: nâng
{
animator.SetTrigger("Up");
AttachPallet();
}
if (Input.GetKeyDown(KeyCode.D)) // Down: hạ
{
animator.SetTrigger("Down");
DetachPallet();
}
}
void AttachPallet()
{
if (currentPallet == null) return;
// Tạo Joint nếu chưa có
if (joint == null)
{
joint = gameObject.AddComponent<FixedJoint>();
joint.connectedBody = currentPallet;
}
}
void DetachPallet()
{
if (joint != null)
{
Destroy(joint); // Tháo pallet
joint = null;
}
currentPallet = null;
}
// Tùy chọn: phát hiện pallet khi vào vùng sensor
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Pallet"))
{
currentPallet = other.attachedRigidbody;
}
}
}