Initial commit
This commit is contained in:
56
Assets/Scripting/ForkSensor.cs
Normal file
56
Assets/Scripting/ForkSensor.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user