Mini-Games-Game/Assets/Scripts/PickUpObject.cs
2024-04-28 19:00:11 +01:00

62 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UIElements;
public class PickUpObject : MonoBehaviour
{
public GameObject carryPosition;
public bool insideTrigger = false;
public bool carrying = false;
public GameObject carriableObject;
public float throwForce;
public KeyCode interactKey = KeyCode.F;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(insideTrigger && Input.GetKeyDown(interactKey) && !carrying)
{
carrying = true;
carriableObject.GetComponent<Rigidbody>().isKinematic = true;
}
else if(carrying && Input.GetKeyDown(interactKey))
{
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(carrying)
{
carriableObject.transform.position = carryPosition.transform.position;
carriableObject.transform.rotation = carryPosition.transform.rotation;
}
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "PickupObject")
{
carriableObject = other.gameObject;
// If player presses D while inside the trigger, pick up the object
insideTrigger = true;
}
}
void OnTriggerExit(Collider other)
{
if(other.gameObject.tag == "PickupObject")
{
insideTrigger = false;
}
}
}