Files
StoreSim/Assets/Scripts/PlayerController.cs

149 lines
5.5 KiB
C#
Raw Permalink Normal View History

2026-05-28 10:16:15 +01:00
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
[SerializeField] InputActionReference moveAction, jumpAction, lookAction, selectAction, discardAction;
[SerializeField] float moveSpeed, lookSpeed, minLookAngle, maxLookAngle, interactionRange, throwForce;
[SerializeField] float jumpForce;
[SerializeField] CharacterController charCon;
[SerializeField] Camera theCam;
[SerializeField] Transform holdPoint;
[SerializeField] LayerMask whatIsStock, whatIsShelf;
private float ySpeed, horiRot, vertRot, currentThrowPower;
private StockObject heldPickup;
private void OnEnable()
{
if (moveAction != null)
moveAction.action.Enable();
if (jumpAction != null)
jumpAction.action.Enable();
if (lookAction != null)
lookAction.action.Enable();
if (selectAction != null)
selectAction.action.Enable();
if (discardAction != null)
discardAction.action.Enable();
}
private void OnDisable()
{
if (moveAction != null)
moveAction.action.Disable();
if (jumpAction != null)
jumpAction.action.Disable();
if (lookAction != null)
lookAction.action.Disable();
if (selectAction != null)
selectAction.action.Disable();
if (discardAction != null)
discardAction.action.Disable();
}
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
Vector2 lookInput = lookAction.action.ReadValue<Vector2>();
horiRot += lookInput.x * Time.deltaTime * lookSpeed;
transform.rotation = Quaternion.Euler(0f, horiRot, 0f);
vertRot -= lookInput.y * Time.deltaTime * lookSpeed;
vertRot = Mathf.Clamp(vertRot, minLookAngle, maxLookAngle);
theCam.transform.localRotation = Quaternion.Euler(vertRot, 0f, 0f);
// Safety check to prevent errors if references are missing in the Inspector
if (moveAction == null || charCon == null) return;
Vector2 moveInput = moveAction.action.ReadValue<Vector2>();
//Debug.Log(moveInput);
//transform.position = transform.position + new Vector3(moveInput.x * Time.deltaTime * moveSpeed, 0f, moveInput.y * Time.deltaTime * moveSpeed);
//Vector3 moveAmount = new Vector3(moveInput.x, 0f, moveInput.y);
Vector3 vertMove = transform.forward * moveInput.y;
Vector3 horiMove = transform.right * moveInput.x;
Vector3 moveAmount = vertMove + horiMove;
moveAmount = moveAmount.normalized * moveSpeed;
if (charCon.isGrounded)
{
ySpeed = 0;
if (jumpAction.action.WasPressedThisFrame())
ySpeed = jumpForce;
}
ySpeed = ySpeed + (Physics.gravity.y * Time.deltaTime);
moveAmount.y = ySpeed;
charCon.Move(moveAmount * Time.deltaTime);
//check for pickup
Ray ray = theCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
RaycastHit hit;
if (heldPickup == null)
{
if(selectAction.action.WasPressedThisFrame())
{
if (Physics.Raycast(ray, out hit, interactionRange, whatIsStock))
{
//Debug.Log("Hit " + hit.collider.name);
heldPickup = hit.collider.GetComponent<StockObject>();
heldPickup.transform.SetParent(holdPoint);
heldPickup.Pickup();
}
}
if (discardAction.action.WasPressedThisFrame())
{
if (Physics.Raycast(ray, out hit, interactionRange, whatIsShelf))
{
heldPickup = hit.collider.GetComponent<ShelfSpaceController>().Getstock();
if (heldPickup != null)
{
heldPickup.transform.SetParent(holdPoint);
heldPickup.Pickup();
}
}
}
}
else if (heldPickup != null)
{
if(selectAction.action.WasPressedThisFrame())
{
if (Physics.Raycast(ray, out hit, interactionRange, whatIsShelf))
{
//heldPickup.MakePlaced();
//heldPickup.transform.SetParent(hit.transform);
//heldPickup = null;
hit.collider.GetComponent<ShelfSpaceController>().PlaceStock(heldPickup);
if(heldPickup.isPlaced)
heldPickup = null;
}
}
// While the button is held, increase the current power up to the max throwForce
if (discardAction.action.IsPressed())
{
currentThrowPower = Mathf.Min(currentThrowPower + (throwForce * Time.deltaTime), throwForce);
}
// When the button is released, either throw or drop
if (discardAction.action.WasReleasedThisFrame())
{
heldPickup.Release();
// If power is above a small threshold (not just a quick click), apply force
if (currentThrowPower > throwForce * 0.1f)
{
heldPickup.theRB.AddForce(theCam.transform.forward * currentThrowPower, ForceMode.Impulse);
}
heldPickup = null;
currentThrowPower = 0f;
}
}
}
}