2024-04-28 19:49:14 +01:00
|
|
|
using System;
|
2024-04-28 15:08:27 +01:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2024-04-28 17:14:08 +01:00
|
|
|
using Unity.VisualScripting;
|
2024-04-28 15:08:27 +01:00
|
|
|
using UnityEngine;
|
2024-04-28 19:49:14 +01:00
|
|
|
using UnityEngine.Animations;
|
2024-04-28 15:08:27 +01:00
|
|
|
using UnityEngine.UIElements;
|
|
|
|
|
|
|
|
public class PickUpObject : MonoBehaviour
|
|
|
|
{
|
2024-04-30 01:52:14 +01:00
|
|
|
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
|
|
|
|
|
2024-04-28 15:08:27 +01:00
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
2024-04-30 01:52:14 +01:00
|
|
|
cooldown = 1;
|
|
|
|
cooldownActive = true;
|
2024-04-28 15:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2024-04-30 01:52:14 +01:00
|
|
|
// 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)
|
2024-04-28 19:49:14 +01:00
|
|
|
{
|
2024-04-30 01:52:14 +01:00
|
|
|
previousObject.GetComponent<Rigidbody>().useGravity = true;
|
2024-04-28 19:49:14 +01:00
|
|
|
}
|
2024-04-30 01:52:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If player is inside trigger and presses F, pick up object
|
|
|
|
if(insideTrigger && Input.GetKeyDown(KeyCode.F) && !cooldownActive)
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
2024-04-30 01:52:14 +01:00
|
|
|
// Disables gravity on object and sets carrying to true
|
|
|
|
carriableObject.GetComponent<Rigidbody>().useGravity = false;
|
2024-04-28 15:08:27 +01:00
|
|
|
carrying = true;
|
2024-04-30 01:52:14 +01:00
|
|
|
// Sets held object to carriable object and sets cooldown to 1
|
|
|
|
heldObject = carriableObject;
|
|
|
|
cooldown = 1;
|
2024-04-28 15:08:27 +01:00
|
|
|
}
|
2024-04-30 01:52:14 +01:00
|
|
|
// If player is carrying object and presses F, drop object
|
2024-04-28 19:49:14 +01:00
|
|
|
else if(carrying && Input.GetKeyDown(KeyCode.F))
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
2024-04-30 01:52:14 +01:00
|
|
|
// Re-enables gravity on object and sets carrying to false and assigns held object to previous object
|
|
|
|
heldObject.GetComponent<Rigidbody>().useGravity = true;
|
|
|
|
previousObject = heldObject;
|
2024-04-28 15:08:27 +01:00
|
|
|
carrying = false;
|
2024-04-28 17:14:08 +01:00
|
|
|
// Drop the object in the direction the player is moving and add a force to it depending on speed
|
2024-04-30 01:52:14 +01:00
|
|
|
previousObject.GetComponent<Rigidbody>().AddForce(transform.forward * throwForce, ForceMode.Impulse);
|
2024-04-28 15:08:27 +01:00
|
|
|
}
|
|
|
|
|
2024-04-30 01:52:14 +01:00
|
|
|
// If player is carrying object, teleport object to carry position
|
2024-04-28 15:08:27 +01:00
|
|
|
if(carrying)
|
|
|
|
{
|
|
|
|
carriableObject.transform.position = carryPosition.transform.position;
|
|
|
|
carriableObject.transform.rotation = carryPosition.transform.rotation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 01:52:14 +01:00
|
|
|
// If player is inside trigger tagged as PickupObject, set carriable object to object inside trigger
|
|
|
|
void OnTriggerStay(Collider other)
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
2024-04-30 01:52:14 +01:00
|
|
|
if(other.gameObject.tag == "PickupObject" && !cooldownActive)
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
|
|
|
carriableObject = other.gameObject;
|
2024-04-30 01:52:14 +01:00
|
|
|
// If player is inside trigger, set inside trigger to true
|
2024-04-28 15:08:27 +01:00
|
|
|
insideTrigger = true;
|
2024-04-30 01:52:14 +01:00
|
|
|
// 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;
|
|
|
|
}
|
2024-04-28 15:08:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-30 01:52:14 +01:00
|
|
|
// If player is not inside trigger, set inside trigger to false
|
2024-04-28 15:08:27 +01:00
|
|
|
void OnTriggerExit(Collider other)
|
|
|
|
{
|
|
|
|
if(other.gameObject.tag == "PickupObject")
|
|
|
|
{
|
|
|
|
insideTrigger = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-04-30 01:52:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
//! 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
|