Initial Unity prototype
This commit is contained in:
commit
138750dd52
386 changed files with 40723 additions and 0 deletions
58
Assets/Scripts/CartPickup.cs
Normal file
58
Assets/Scripts/CartPickup.cs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class CartPickup : MonoBehaviour
|
||||
{
|
||||
public float pickupRange = 3f;
|
||||
public Transform holdPoint; // An empty GameObject in front of player
|
||||
private GameObject heldObject;
|
||||
private bool isHolding = false;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.E))
|
||||
{
|
||||
if (isHolding)
|
||||
{
|
||||
DropObject();
|
||||
}
|
||||
else
|
||||
{
|
||||
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, pickupRange))
|
||||
{
|
||||
if (hit.collider.CompareTag("Cart"))
|
||||
{
|
||||
PickupObject(hit.collider.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Optional: Keep the cart tightly in position
|
||||
if (isHolding && heldObject != null)
|
||||
{
|
||||
heldObject.transform.position = holdPoint.position;
|
||||
heldObject.transform.rotation = holdPoint.rotation;
|
||||
}
|
||||
}
|
||||
|
||||
void PickupObject(GameObject obj)
|
||||
{
|
||||
heldObject = obj;
|
||||
isHolding = true;
|
||||
obj.GetComponent<Rigidbody>().isKinematic = true;
|
||||
obj.transform.SetParent(holdPoint);
|
||||
obj.transform.localPosition = Vector3.zero;
|
||||
}
|
||||
|
||||
void DropObject()
|
||||
{
|
||||
if (heldObject != null)
|
||||
{
|
||||
heldObject.transform.SetParent(null);
|
||||
heldObject.GetComponent<Rigidbody>().isKinematic = false;
|
||||
heldObject = null;
|
||||
}
|
||||
isHolding = false;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue