2024-04-28 19:49:14 +01:00
|
|
|
using System;
|
2024-04-28 15:08:27 +01:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2024-04-28 17:14:08 +01:00
|
|
|
using Unity.VisualScripting;
|
2024-04-28 15:08:27 +01:00
|
|
|
using UnityEngine;
|
2024-04-28 19:49:14 +01:00
|
|
|
using UnityEngine.Animations;
|
2024-04-28 15:08:27 +01:00
|
|
|
using UnityEngine.UIElements;
|
|
|
|
|
2024-06-13 01:52:43 +01:00
|
|
|
//* General Comments or Finished Tasks
|
|
|
|
//TODO Tasks left to be done for game
|
|
|
|
//! Bugs or Issues
|
|
|
|
//? Questions or Suggestions
|
|
|
|
|
2024-06-13 02:19:12 +01:00
|
|
|
//! Somtimes boxes get thrown further than other times. Boxes also sometimes get thrown into random directions.
|
2024-04-28 15:08:27 +01:00
|
|
|
public class PickUpObject : MonoBehaviour
|
|
|
|
{
|
2024-04-30 01:52:14 +01:00
|
|
|
public GameObject carryPosition; // blank object, position declares where the object will be teleported to/carried
|
|
|
|
public bool insideTrigger = false; // checks if player is inside the trigger of a pickup object
|
2024-04-30 14:26:58 +01:00
|
|
|
public bool playerIsCarrying = false; // checks if player is carrying an object
|
|
|
|
public GameObject nearestCarriableObject;
|
2024-04-30 01:52:14 +01:00
|
|
|
public GameObject previousObject; // previous object that player was carrying
|
|
|
|
public GameObject heldObject; // object that player is currently holding
|
2024-04-30 14:26:58 +01:00
|
|
|
public float throwForce;
|
|
|
|
/// <summary>
|
|
|
|
/// Cooldown time between picking up objects in seconds
|
|
|
|
/// </summary>
|
|
|
|
public float cooldownSeconds = 1;
|
|
|
|
public bool cooldownActive = true;
|
|
|
|
public KeyCode interactKey = KeyCode.F;
|
2024-04-30 01:52:14 +01:00
|
|
|
|
2024-04-28 15:08:27 +01:00
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
2024-04-30 14:26:58 +01:00
|
|
|
|
2024-04-28 15:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2024-04-30 14:26:58 +01:00
|
|
|
// * Cubes are quite hot so need time to cool off
|
|
|
|
cooldownSeconds -= Time.deltaTime;
|
2024-04-30 19:45:02 +01:00
|
|
|
cooldownActive = cooldownSeconds > 0;
|
2024-04-30 01:52:14 +01:00
|
|
|
|
2024-04-30 14:26:58 +01:00
|
|
|
// * Old objects deserve gravity too
|
|
|
|
if(previousObject != null && previousObject.GetComponent<Rigidbody>().useGravity == false)
|
2024-04-30 01:52:14 +01:00
|
|
|
{
|
2024-04-30 14:26:58 +01:00
|
|
|
previousObject.GetComponent<Rigidbody>().useGravity = true;
|
2024-04-30 01:52:14 +01:00
|
|
|
}
|
2024-04-30 19:45:02 +01:00
|
|
|
|
2024-04-30 01:52:14 +01:00
|
|
|
if(insideTrigger && Input.GetKeyDown(KeyCode.F) && !cooldownActive)
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
2024-04-30 14:26:58 +01:00
|
|
|
// * Who needs gravity
|
|
|
|
nearestCarriableObject.GetComponent<Rigidbody>().useGravity = false;
|
|
|
|
playerIsCarrying = true;
|
|
|
|
heldObject = nearestCarriableObject;
|
|
|
|
cooldownSeconds = 1;
|
2024-04-28 15:08:27 +01:00
|
|
|
}
|
2024-04-30 14:26:58 +01:00
|
|
|
// * YEET
|
|
|
|
else if(playerIsCarrying && Input.GetKeyDown(KeyCode.F))
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
2024-04-30 01:52:14 +01:00
|
|
|
heldObject.GetComponent<Rigidbody>().useGravity = true;
|
|
|
|
previousObject = heldObject;
|
2024-04-30 14:26:58 +01:00
|
|
|
playerIsCarrying = false;
|
2024-04-30 01:52:14 +01:00
|
|
|
previousObject.GetComponent<Rigidbody>().AddForce(transform.forward * throwForce, ForceMode.Impulse);
|
2024-04-30 19:45:02 +01:00
|
|
|
Debug.Log("Yoted!");
|
2024-04-28 15:08:27 +01:00
|
|
|
}
|
|
|
|
|
2024-04-30 14:26:58 +01:00
|
|
|
// * Cubes not stored above heads are highly dangerous
|
|
|
|
if(playerIsCarrying)
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
2024-04-30 14:26:58 +01:00
|
|
|
nearestCarriableObject.transform.position = carryPosition.transform.position;
|
|
|
|
nearestCarriableObject.transform.rotation = carryPosition.transform.rotation;
|
2024-04-28 15:08:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 02:19:12 +01:00
|
|
|
//* Called when the player is inside a trigger tagged as PickupObject
|
2024-04-30 14:26:58 +01:00
|
|
|
void OnTriggerStay(Collider otherObject)
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
2024-04-30 14:26:58 +01:00
|
|
|
if(otherObject.gameObject.tag == "PickupObject" && !cooldownActive)
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
2024-04-30 14:26:58 +01:00
|
|
|
nearestCarriableObject = otherObject.gameObject;
|
2024-04-28 15:08:27 +01:00
|
|
|
insideTrigger = true;
|
2024-04-30 14:26:58 +01:00
|
|
|
// * old cube is boring
|
|
|
|
if(heldObject != null && heldObject != nearestCarriableObject && playerIsCarrying)
|
2024-04-30 01:52:14 +01:00
|
|
|
{
|
|
|
|
previousObject = heldObject;
|
2024-04-30 14:26:58 +01:00
|
|
|
cooldownSeconds = 1;
|
2024-04-30 01:52:14 +01:00
|
|
|
}
|
2024-04-28 15:08:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 02:19:12 +01:00
|
|
|
//* If player is not inside trigger, then they probably aren't inside it
|
2024-04-30 14:26:58 +01:00
|
|
|
void OnTriggerExit(Collider otherObject)
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
2024-04-30 14:26:58 +01:00
|
|
|
if(otherObject.gameObject.tag == "PickupObject")
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
|
|
|
insideTrigger = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-30 01:52:14 +01:00
|
|
|
|
|
|
|
//! To make player launch self:
|
|
|
|
//! add public GameObject player; and place player in it fron inspector
|
|
|
|
//! in if(insiderTrigger && Input.GetKeyDown(KeyCode.F))
|
|
|
|
//! Place the code below:
|
|
|
|
//! player.GetComponent<Rigidbody>().AddForce(transform.forward * throwForce, ForceMode.Impulse);
|
|
|
|
//! Run the game and press F to launch the player
|