Initial Unity prototype
This commit is contained in:
commit
138750dd52
386 changed files with 40723 additions and 0 deletions
54
Assets/Scripts/CartGrabAndPush.cs
Normal file
54
Assets/Scripts/CartGrabAndPush.cs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class CartGrabAndHold : MonoBehaviour
|
||||
{
|
||||
public float holdDistance = 1.5f; // Distance in front of player
|
||||
public float moveSpeed = 5f; // How fast the cart follows
|
||||
public float floatHeight = 0.5f; // Height above the ground to simulate wheels
|
||||
|
||||
private bool isHolding = false; // Whether the cart is currently held
|
||||
private Transform player; // The transform of the player
|
||||
private Rigidbody rb; // Cart's rigidbody
|
||||
|
||||
void Start()
|
||||
{
|
||||
rb = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
// Called by PlayerGrabber when grabbing
|
||||
public void GrabCart(Transform playerTransform)
|
||||
{
|
||||
isHolding = true;
|
||||
player = playerTransform;
|
||||
rb.isKinematic = true; // Disable physics for smooth movement
|
||||
}
|
||||
|
||||
// Called by PlayerGrabber when releasing
|
||||
public void ReleaseCart()
|
||||
{
|
||||
isHolding = false;
|
||||
rb.isKinematic = false; // Re-enable physics
|
||||
player = null;
|
||||
}
|
||||
|
||||
// Used by PlayerGrabber to keep cart following the player
|
||||
public void HoldCart()
|
||||
{
|
||||
if (!isHolding || player == null) return;
|
||||
|
||||
// Target position is in front of the player on X/Z, floating above ground
|
||||
Vector3 targetPosition = player.position + player.forward * holdDistance;
|
||||
targetPosition.y = floatHeight;
|
||||
|
||||
// Smoothly move cart toward target
|
||||
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * moveSpeed);
|
||||
|
||||
// Face the same direction as the player
|
||||
transform.rotation = Quaternion.LookRotation(player.forward);
|
||||
}
|
||||
|
||||
public bool IsHolding()
|
||||
{
|
||||
return isHolding;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/CartGrabAndPush.cs.meta
Normal file
11
Assets/Scripts/CartGrabAndPush.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 440c1e39b1656d3429dfd613d4616cae
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
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;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/CartPickup.cs.meta
Normal file
11
Assets/Scripts/CartPickup.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 12a78c80967b2c6439280277ea1dfe85
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
23
Assets/Scripts/CartReturnZone.cs
Normal file
23
Assets/Scripts/CartReturnZone.cs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
using UnityEngine;
|
||||
using TMPro; // Import for TextMeshPro
|
||||
|
||||
public class CartReturnZone : MonoBehaviour
|
||||
{
|
||||
public TMP_Text scoreText; // Reference to the TMP text
|
||||
public AudioSource returnSound; // Sound to play when cart is returned
|
||||
private int score = 0;
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (other.CompareTag("Cart"))
|
||||
{
|
||||
score++;
|
||||
scoreText.text = "Score: " + score; // Update the TMP UI
|
||||
|
||||
returnSound.Play(); // Play sound when cart is returned
|
||||
|
||||
// Optional: Destroy the cart or move it away
|
||||
Destroy(other.gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/CartReturnZone.cs.meta
Normal file
11
Assets/Scripts/CartReturnZone.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5b73963a02ad0f94aae2179c928ffa61
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
44
Assets/Scripts/PlayerGrabber.cs
Normal file
44
Assets/Scripts/PlayerGrabber.cs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class PlayerGrabber : MonoBehaviour
|
||||
{
|
||||
public float grabDistance = 2f;
|
||||
private CartGrabAndHold heldCart;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.E))
|
||||
{
|
||||
if (heldCart == null)
|
||||
{
|
||||
TryGrabCart();
|
||||
}
|
||||
else
|
||||
{
|
||||
heldCart.ReleaseCart();
|
||||
heldCart = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (heldCart != null)
|
||||
{
|
||||
heldCart.HoldCart(); // Let it keep following while held
|
||||
}
|
||||
}
|
||||
|
||||
void TryGrabCart()
|
||||
{
|
||||
Ray ray = new Ray(transform.position, transform.forward);
|
||||
RaycastHit hit;
|
||||
|
||||
if (Physics.Raycast(ray, out hit, grabDistance))
|
||||
{
|
||||
CartGrabAndHold cart = hit.collider.GetComponent<CartGrabAndHold>();
|
||||
if (cart != null)
|
||||
{
|
||||
heldCart = cart;
|
||||
cart.GrabCart(transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PlayerGrabber.cs.meta
Normal file
11
Assets/Scripts/PlayerGrabber.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: a504616348cd466448a3e621e548bc6c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Add table
Add a link
Reference in a new issue