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": [
"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 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 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; // force that player throws object with
public float cooldown; // time between picking up objects
public bool cooldownActive; // checks if cooldown is active
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;
// Start is called before the first frame update
void Start()
{
cooldown = 1;
cooldownActive = true;
}
// Update is called once per frame
void Update()
{
// Checks if timer is below 0, if so, turns cooldownActive off, if not, activates cooldown
cooldown -= Time.deltaTime;
if(cooldown <= 0)
{
cooldownActive = false;
}
else
{
cooldownActive = true;
}
// * Cubes are quite hot so need time to cool off
cooldownSeconds -= Time.deltaTime;
cooldownActive = cooldownSeconds > 0
// If previous object is not held object or null, applies gravity to it
if(previousObject != null)
{
if(previousObject.GetComponent<Rigidbody>().useGravity == false)
// * Old objects deserve gravity too
if(previousObject != null && 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;
// * Who needs gravity
nearestCarriableObject.GetComponent<Rigidbody>().useGravity = false;
playerIsCarrying = true;
heldObject = nearestCarriableObject;
cooldownSeconds = 1;
}
// If player is carrying object and presses F, drop object
else if(carrying && Input.GetKeyDown(KeyCode.F))
// * YEET
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;
previousObject = heldObject;
carrying = false;
// Drop the object in the direction the player is moving and add a force to it depending on speed
playerIsCarrying = false;
previousObject.GetComponent<Rigidbody>().AddForce(transform.forward * throwForce, ForceMode.Impulse);
Debug.Log("Yoted!")
}
// If player is carrying object, teleport object to carry position
if(carrying)
// * Cubes not stored above heads are highly dangerous
if(playerIsCarrying)
{
carriableObject.transform.position = carryPosition.transform.position;
carriableObject.transform.rotation = carryPosition.transform.rotation;
nearestCarriableObject.transform.position = carryPosition.transform.position;
nearestCarriableObject.transform.rotation = carryPosition.transform.rotation;
}
}
// If player is inside trigger tagged as PickupObject, set carriable object to object inside trigger
void OnTriggerStay(Collider other)
// Called when the player is inside a trigger tagged as PickupObject
void OnTriggerStay(Collider otherObject)
{
if(other.gameObject.tag == "PickupObject" && !cooldownActive)
if(otherObject.gameObject.tag == "PickupObject" && !cooldownActive)
{
carriableObject = other.gameObject;
// If player is inside trigger, set inside trigger to true
nearestCarriableObject = otherObject.gameObject;
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)
// * old cube is boring
if(heldObject != null && heldObject != nearestCarriableObject && playerIsCarrying)
{
previousObject = heldObject;
cooldown = 1;
cooldownSeconds = 1;
}
}
}
// If player is not inside trigger, set inside trigger to false
void OnTriggerExit(Collider other)
// If player is not inside trigger, then they probably aren't inside it
void OnTriggerExit(Collider otherObject)
{
if(other.gameObject.tag == "PickupObject")
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))