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
This commit is contained in:
parent
35dd1d8902
commit
37dbe393f5
@ -466,8 +466,6 @@ MonoBehaviour:
|
|||||||
jumpForce: 2.5
|
jumpForce: 2.5
|
||||||
gravity: 9.8
|
gravity: 9.8
|
||||||
throwForce: {fileID: 765999536}
|
throwForce: {fileID: 765999536}
|
||||||
controller: {fileID: 765999538}
|
|
||||||
moveDirection: {x: 0, y: 0, z: 0}
|
|
||||||
forwardsKey: 119
|
forwardsKey: 119
|
||||||
backwardsKey: 115
|
backwardsKey: 115
|
||||||
leftKey: 97
|
leftKey: 97
|
||||||
@ -493,6 +491,8 @@ MonoBehaviour:
|
|||||||
previousObject: {fileID: 0}
|
previousObject: {fileID: 0}
|
||||||
tempObject: {fileID: 0}
|
tempObject: {fileID: 0}
|
||||||
throwForce: 5
|
throwForce: 5
|
||||||
|
cooldown: 0
|
||||||
|
cooldownActive: 0
|
||||||
--- !u!54 &765999537
|
--- !u!54 &765999537
|
||||||
Rigidbody:
|
Rigidbody:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
@ -556,7 +556,7 @@ Transform:
|
|||||||
m_PrefabAsset: {fileID: 0}
|
m_PrefabAsset: {fileID: 0}
|
||||||
m_GameObject: {fileID: 876529135}
|
m_GameObject: {fileID: 876529135}
|
||||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
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_LocalScale: {x: 1, y: 1, z: 1}
|
||||||
m_ConstrainProportionsScale: 0
|
m_ConstrainProportionsScale: 0
|
||||||
m_Children: []
|
m_Children: []
|
||||||
@ -763,7 +763,7 @@ Transform:
|
|||||||
m_PrefabAsset: {fileID: 0}
|
m_PrefabAsset: {fileID: 0}
|
||||||
m_GameObject: {fileID: 1091937918}
|
m_GameObject: {fileID: 1091937918}
|
||||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
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_LocalScale: {x: 1, y: 1, z: 1}
|
||||||
m_ConstrainProportionsScale: 0
|
m_ConstrainProportionsScale: 0
|
||||||
m_Children: []
|
m_Children: []
|
||||||
@ -1183,7 +1183,7 @@ Transform:
|
|||||||
m_PrefabAsset: {fileID: 0}
|
m_PrefabAsset: {fileID: 0}
|
||||||
m_GameObject: {fileID: 1846846724}
|
m_GameObject: {fileID: 1846846724}
|
||||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
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_LocalScale: {x: 1, y: 1, z: 1}
|
||||||
m_ConstrainProportionsScale: 0
|
m_ConstrainProportionsScale: 0
|
||||||
m_Children: []
|
m_Children: []
|
||||||
|
@ -8,46 +8,68 @@ using UnityEngine.UIElements;
|
|||||||
|
|
||||||
public class PickUpObject : MonoBehaviour
|
public class PickUpObject : MonoBehaviour
|
||||||
{
|
{
|
||||||
public GameObject carryPosition;
|
public GameObject carryPosition; // blank object, position declares where the object will be teleported to/carried
|
||||||
public bool insideTrigger = false;
|
public bool insideTrigger = false; // checks if player is inside the trigger of a pickup object
|
||||||
public bool carrying = false;
|
public bool carrying = false; // checks if player is carrying an object
|
||||||
public GameObject carriableObject;
|
public GameObject carriableObject; // object newest object that player can carry
|
||||||
public GameObject previousObject;
|
public GameObject previousObject; // previous object that player was carrying
|
||||||
public GameObject tempObject;
|
public GameObject heldObject; // object that player is currently holding
|
||||||
public float throwForce;
|
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
|
// Start is called before the first frame update
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
|
cooldown = 1;
|
||||||
|
cooldownActive = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
if(tempObject != carriableObject)
|
// Checks if timer is below 0, if so, turns cooldownActive off, if not, activates cooldown
|
||||||
{
|
cooldown -= Time.deltaTime;
|
||||||
previousObject = tempObject;
|
if(cooldown <= 0)
|
||||||
tempObject = carriableObject;
|
|
||||||
previousObject.GetComponent<Rigidbody>().isKinematic = false;
|
|
||||||
}
|
|
||||||
if(insideTrigger && Input.GetKeyDown(KeyCode.F) && !carrying)
|
|
||||||
{
|
{
|
||||||
carrying = true;
|
cooldownActive = false;
|
||||||
carriableObject.GetComponent<Rigidbody>().isKinematic = true;
|
|
||||||
if(tempObject == null)
|
|
||||||
{
|
|
||||||
tempObject = carriableObject;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if(carrying && Input.GetKeyDown(KeyCode.F))
|
else
|
||||||
{
|
{
|
||||||
tempObject = carriableObject;
|
cooldownActive = true;
|
||||||
carrying = false;
|
|
||||||
carriableObject.GetComponent<Rigidbody>().isKinematic = false;
|
|
||||||
// Drop the object in the direction the player is moving and add a force to it depending on speed
|
|
||||||
carriableObject.GetComponent<Rigidbody>().AddForce(transform.forward * throwForce, ForceMode.Impulse);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If previous object is not held object or null, applies gravity to it
|
||||||
|
if(previousObject != null)
|
||||||
|
{
|
||||||
|
if(previousObject.GetComponent<Rigidbody>().useGravity == false)
|
||||||
|
{
|
||||||
|
previousObject.GetComponent<Rigidbody>().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<Rigidbody>().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<Rigidbody>().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<Rigidbody>().AddForce(transform.forward * throwForce, ForceMode.Impulse);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If player is carrying object, teleport object to carry position
|
||||||
if(carrying)
|
if(carrying)
|
||||||
{
|
{
|
||||||
carriableObject.transform.position = carryPosition.transform.position;
|
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;
|
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;
|
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)
|
void OnTriggerExit(Collider other)
|
||||||
{
|
{
|
||||||
if(other.gameObject.tag == "PickupObject")
|
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<Rigidbody>().AddForce(transform.forward * throwForce, ForceMode.Impulse);
|
||||||
|
//! Run the game and press F to launch the player
|
Loading…
Reference in New Issue
Block a user