Refactor: Changes to comments and variable names

Changed:
- Comments
- Variable names

Added:
- Better Comments to the list of recommended extensions
This commit is contained in:
Arlo Filley 2024-04-30 14:26:58 +01:00
parent 37dbe393f5
commit 54cb3b0159
2 changed files with 42 additions and 53 deletions

View File

@ -1,5 +1,6 @@
{ {
"recommendations": [ "recommendations": [
"visualstudiotoolsforunity.vstuc" "visualstudiotoolsforunity.vstuc",
"aaron-bond.better-comments"
] ]
} }

View File

@ -10,101 +10,89 @@ public class PickUpObject : MonoBehaviour
{ {
public GameObject carryPosition; // blank object, position declares where the object will be teleported to/carried 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 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 bool playerIsCarrying = false; // checks if player is carrying an object
public GameObject carriableObject; // object newest object that player can carry public GameObject nearestCarriableObject;
public GameObject previousObject; // previous object that player was carrying public GameObject previousObject; // previous object that player was carrying
public GameObject heldObject; // object that player is currently holding public GameObject heldObject; // object that player is currently holding
public float throwForce; // force that player throws object with public float throwForce;
public float cooldown; // time between picking up objects /// <summary>
public bool cooldownActive; // checks if cooldown is active /// Cooldown time between picking up objects in seconds
/// </summary>
public float cooldownSeconds = 1;
public bool cooldownActive = true;
public KeyCode interactKey = KeyCode.F;
// 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()
{ {
// Checks if timer is below 0, if so, turns cooldownActive off, if not, activates cooldown // * Cubes are quite hot so need time to cool off
cooldown -= Time.deltaTime; cooldownSeconds -= Time.deltaTime;
if(cooldown <= 0) cooldownActive = cooldownSeconds > 0
// * Old objects deserve gravity too
if(previousObject != null && previousObject.GetComponent<Rigidbody>().useGravity == false)
{ {
cooldownActive = false; previousObject.GetComponent<Rigidbody>().useGravity = true;
}
else
{
cooldownActive = true;
} }
// 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) if(insideTrigger && Input.GetKeyDown(KeyCode.F) && !cooldownActive)
{ {
// Disables gravity on object and sets carrying to true // * Who needs gravity
carriableObject.GetComponent<Rigidbody>().useGravity = false; nearestCarriableObject.GetComponent<Rigidbody>().useGravity = false;
carrying = true; playerIsCarrying = true;
// Sets held object to carriable object and sets cooldown to 1 heldObject = nearestCarriableObject;
heldObject = carriableObject; cooldownSeconds = 1;
cooldown = 1;
} }
// If player is carrying object and presses F, drop object // * YEET
else if(carrying && Input.GetKeyDown(KeyCode.F)) else if(playerIsCarrying && 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; heldObject.GetComponent<Rigidbody>().useGravity = true;
previousObject = heldObject; previousObject = heldObject;
carrying = false; playerIsCarrying = 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); previousObject.GetComponent<Rigidbody>().AddForce(transform.forward * throwForce, ForceMode.Impulse);
Debug.Log("Yoted!")
} }
// If player is carrying object, teleport object to carry position // * Cubes not stored above heads are highly dangerous
if(carrying) if(playerIsCarrying)
{ {
carriableObject.transform.position = carryPosition.transform.position; nearestCarriableObject.transform.position = carryPosition.transform.position;
carriableObject.transform.rotation = carryPosition.transform.rotation; nearestCarriableObject.transform.rotation = carryPosition.transform.rotation;
} }
} }
// If player is inside trigger tagged as PickupObject, set carriable object to object inside trigger // Called when the player is inside a trigger tagged as PickupObject
void OnTriggerStay(Collider other) void OnTriggerStay(Collider otherObject)
{ {
if(other.gameObject.tag == "PickupObject" && !cooldownActive) if(otherObject.gameObject.tag == "PickupObject" && !cooldownActive)
{ {
carriableObject = other.gameObject; nearestCarriableObject = otherObject.gameObject;
// 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 // * old cube is boring
if(heldObject != carriableObject && heldObject != null && carrying) if(heldObject != null && heldObject != nearestCarriableObject && playerIsCarrying)
{ {
previousObject = heldObject; previousObject = heldObject;
cooldown = 1; cooldownSeconds = 1;
} }
} }
} }
// If player is not inside trigger, set inside trigger to false // If player is not inside trigger, then they probably aren't inside it
void OnTriggerExit(Collider other) void OnTriggerExit(Collider otherObject)
{ {
if(other.gameObject.tag == "PickupObject") if(otherObject.gameObject.tag == "PickupObject")
{ {
insideTrigger = false; insideTrigger = false;
} }
} }
} }
//! To make player launch self: //! To make player launch self:
//! add public GameObject player; and place player in it fron inspector //! add public GameObject player; and place player in it fron inspector
//! in if(insiderTrigger && Input.GetKeyDown(KeyCode.F)) //! in if(insiderTrigger && Input.GetKeyDown(KeyCode.F))