From 37dbe393f587be5c69f0766b655dc31a84cbf86e Mon Sep 17 00:00:00 2001 From: Santi Date: Tue, 30 Apr 2024 01:52:14 +0100 Subject: [PATCH] Unity: Created a cooldown for cube pickup. Added comments Added a 1 second cooldown that prevents the player from picking up muptile cubes in under 1 second Commented code explaining how it works. Commented in red how to make player go flying if needed Player does still get shot into the sky randomly while holding an item - must find cause and fix --- Assets/Scenes/Character.unity | 10 ++-- Assets/Scripts/PickUpObject.cs | 98 +++++++++++++++++++++++----------- 2 files changed, 73 insertions(+), 35 deletions(-) diff --git a/Assets/Scenes/Character.unity b/Assets/Scenes/Character.unity index 13b55a8..029e470 100644 --- a/Assets/Scenes/Character.unity +++ b/Assets/Scenes/Character.unity @@ -466,8 +466,6 @@ MonoBehaviour: jumpForce: 2.5 gravity: 9.8 throwForce: {fileID: 765999536} - controller: {fileID: 765999538} - moveDirection: {x: 0, y: 0, z: 0} forwardsKey: 119 backwardsKey: 115 leftKey: 97 @@ -493,6 +491,8 @@ MonoBehaviour: previousObject: {fileID: 0} tempObject: {fileID: 0} throwForce: 5 + cooldown: 0 + cooldownActive: 0 --- !u!54 &765999537 Rigidbody: m_ObjectHideFlags: 0 @@ -556,7 +556,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 876529135} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -7.72, y: 0.44, z: 2.84} + m_LocalPosition: {x: -7.45, y: 0.44, z: 0.93} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] @@ -763,7 +763,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1091937918} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 1.6, z: 0} + m_LocalPosition: {x: 0, y: 2.01, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] @@ -1183,7 +1183,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1846846724} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -6.52, y: 0.94, z: -5.65} + m_LocalPosition: {x: -7, y: 0.94, z: -2.01} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: [] diff --git a/Assets/Scripts/PickUpObject.cs b/Assets/Scripts/PickUpObject.cs index 7551db5..0860203 100644 --- a/Assets/Scripts/PickUpObject.cs +++ b/Assets/Scripts/PickUpObject.cs @@ -8,46 +8,68 @@ using UnityEngine.UIElements; public class PickUpObject : MonoBehaviour { - public GameObject carryPosition; - public bool insideTrigger = false; - public bool carrying = false; - public GameObject carriableObject; - public GameObject previousObject; - public GameObject tempObject; - public float throwForce; + 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 carrying = false; // checks if player is carrying an object + public GameObject carriableObject; // object newest object that player can carry + public GameObject previousObject; // previous object that player was carrying + public GameObject heldObject; // object that player is currently holding + public float throwForce; // force that player throws object with + public float cooldown; // time between picking up objects + public bool cooldownActive; // checks if cooldown is active + // Start is called before the first frame update void Start() { - + cooldown = 1; + cooldownActive = true; } // Update is called once per frame void Update() { - if(tempObject != carriableObject) - { - previousObject = tempObject; - tempObject = carriableObject; - previousObject.GetComponent().isKinematic = false; - } - if(insideTrigger && Input.GetKeyDown(KeyCode.F) && !carrying) + // Checks if timer is below 0, if so, turns cooldownActive off, if not, activates cooldown + cooldown -= Time.deltaTime; + if(cooldown <= 0) { - carrying = true; - carriableObject.GetComponent().isKinematic = true; - if(tempObject == null) - { - tempObject = carriableObject; - } + cooldownActive = false; } - else if(carrying && Input.GetKeyDown(KeyCode.F)) + else { - tempObject = carriableObject; - carrying = false; - carriableObject.GetComponent().isKinematic = false; - // Drop the object in the direction the player is moving and add a force to it depending on speed - carriableObject.GetComponent().AddForce(transform.forward * throwForce, ForceMode.Impulse); + cooldownActive = true; } + // If previous object is not held object or null, applies gravity to it + if(previousObject != null) + { + if(previousObject.GetComponent().useGravity == false) + { + previousObject.GetComponent().useGravity = true; + } + } + + // If player is inside trigger and presses F, pick up object + if(insideTrigger && Input.GetKeyDown(KeyCode.F) && !cooldownActive) + { + // Disables gravity on object and sets carrying to true + carriableObject.GetComponent().useGravity = false; + carrying = true; + // Sets held object to carriable object and sets cooldown to 1 + heldObject = carriableObject; + cooldown = 1; + } + // If player is carrying object and presses F, drop object + else if(carrying && Input.GetKeyDown(KeyCode.F)) + { + // Re-enables gravity on object and sets carrying to false and assigns held object to previous object + heldObject.GetComponent().useGravity = true; + previousObject = heldObject; + carrying = false; + // Drop the object in the direction the player is moving and add a force to it depending on speed + previousObject.GetComponent().AddForce(transform.forward * throwForce, ForceMode.Impulse); + } + + // If player is carrying object, teleport object to carry position if(carrying) { carriableObject.transform.position = carryPosition.transform.position; @@ -55,16 +77,24 @@ public class PickUpObject : MonoBehaviour } } - void OnTriggerEnter(Collider other) + // If player is inside trigger tagged as PickupObject, set carriable object to object inside trigger + void OnTriggerStay(Collider other) { - if(other.gameObject.tag == "PickupObject") + if(other.gameObject.tag == "PickupObject" && !cooldownActive) { carriableObject = other.gameObject; - // If player presses D while inside the trigger, pick up the object + // If player is inside trigger, set inside trigger to true insideTrigger = true; + // If player is holding an object and it is not the carriable object, set previous object to held object and set cooldown to 1 + if(heldObject != carriableObject && heldObject != null && carrying) + { + previousObject = heldObject; + cooldown = 1; + } } } + // If player is not inside trigger, set inside trigger to false void OnTriggerExit(Collider other) { if(other.gameObject.tag == "PickupObject") @@ -73,3 +103,11 @@ public class PickUpObject : MonoBehaviour } } } + + +//! 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 \ No newline at end of file