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;
|
|
|
|
using UnityEngine.UIElements;
|
|
|
|
|
|
|
|
public class PickUpObject : MonoBehaviour
|
|
|
|
{
|
|
|
|
public GameObject carryPosition;
|
|
|
|
public bool insideTrigger = false;
|
|
|
|
public bool carrying = false;
|
|
|
|
public GameObject carriableObject;
|
2024-04-28 17:14:08 +01:00
|
|
|
public float throwForce;
|
2024-04-28 19:00:11 +01:00
|
|
|
public KeyCode interactKey = KeyCode.F;
|
2024-04-28 15:08:27 +01:00
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2024-04-28 19:00:11 +01:00
|
|
|
if(insideTrigger && Input.GetKeyDown(interactKey) && !carrying)
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
|
|
|
carrying = true;
|
|
|
|
carriableObject.GetComponent<Rigidbody>().isKinematic = true;
|
|
|
|
}
|
2024-04-28 19:00:11 +01:00
|
|
|
else if(carrying && Input.GetKeyDown(interactKey))
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
|
|
|
carrying = false;
|
|
|
|
carriableObject.GetComponent<Rigidbody>().isKinematic = 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
|
|
|
|
carriableObject.GetComponent<Rigidbody>().AddForce(transform.forward * throwForce, ForceMode.Impulse);
|
2024-04-28 15:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(carrying)
|
|
|
|
{
|
|
|
|
carriableObject.transform.position = carryPosition.transform.position;
|
|
|
|
carriableObject.transform.rotation = carryPosition.transform.rotation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnTriggerEnter(Collider other)
|
|
|
|
{
|
2024-04-28 17:14:08 +01:00
|
|
|
if(other.gameObject.tag == "PickupObject")
|
2024-04-28 15:08:27 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|