using System; using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.Animations; using UnityEngine.UIElements; public class PickUpObject : MonoBehaviour { 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 public bool playerIsCarrying = false; // checks if player is carrying an object public GameObject nearestCarriableObject; public GameObject previousObject; // previous object that player was carrying public GameObject heldObject; // object that player is currently holding public float throwForce; /// /// Cooldown time between picking up objects in seconds /// public float cooldownSeconds = 1; public bool cooldownActive = true; public KeyCode interactKey = KeyCode.F; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { // * Cubes are quite hot so need time to cool off cooldownSeconds -= Time.deltaTime; cooldownActive = cooldownSeconds > 0; // * Old objects deserve gravity too if(previousObject != null && previousObject.GetComponent().useGravity == false) { previousObject.GetComponent().useGravity = true; } if(insideTrigger && Input.GetKeyDown(KeyCode.F) && !cooldownActive) { // * Who needs gravity nearestCarriableObject.GetComponent().useGravity = false; playerIsCarrying = true; heldObject = nearestCarriableObject; cooldownSeconds = 1; } // * YEET else if(playerIsCarrying && Input.GetKeyDown(KeyCode.F)) { heldObject.GetComponent().useGravity = true; previousObject = heldObject; playerIsCarrying = false; previousObject.GetComponent().AddForce(transform.forward * throwForce, ForceMode.Impulse); Debug.Log("Yoted!"); } // * Cubes not stored above heads are highly dangerous if(playerIsCarrying) { nearestCarriableObject.transform.position = carryPosition.transform.position; nearestCarriableObject.transform.rotation = carryPosition.transform.rotation; } } // Called when the player is inside a trigger tagged as PickupObject void OnTriggerStay(Collider otherObject) { if(otherObject.gameObject.tag == "PickupObject" && !cooldownActive) { nearestCarriableObject = otherObject.gameObject; insideTrigger = true; // * old cube is boring if(heldObject != null && heldObject != nearestCarriableObject && playerIsCarrying) { previousObject = heldObject; cooldownSeconds = 1; } } } // If player is not inside trigger, then they probably aren't inside it void OnTriggerExit(Collider otherObject) { if(otherObject.gameObject.tag == "PickupObject") { insideTrigger = false; } } } //! 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().AddForce(transform.forward * throwForce, ForceMode.Impulse); //! Run the game and press F to launch the player