Mini-Games-Game/Assets/Scripts/PickUpObject.cs

113 lines
4.3 KiB
C#
Raw Normal View History

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 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()
{
// 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;
}
// 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)
{
carriableObject.transform.position = carryPosition.transform.position;
carriableObject.transform.rotation = carryPosition.transform.rotation;
}
}
// If player is inside trigger tagged as PickupObject, set carriable object to object inside trigger
void OnTriggerStay(Collider other)
{
if(other.gameObject.tag == "PickupObject" && !cooldownActive)
{
carriableObject = other.gameObject;
// 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")
{
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<Rigidbody>().AddForce(transform.forward * throwForce, ForceMode.Impulse);
//! Run the game and press F to launch the player