Santi
3ffb61b5a9
Objects tagged with PickableObjects can be picked up by the player. The object is moved above the player's head, multiple objects cannot be picked up and objects can be placed down again Added textures from the Unity Asset Store For Testing Grounds
57 lines
1.5 KiB
C#
57 lines
1.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class PickUpObject : MonoBehaviour
|
|
{
|
|
public GameObject carryPosition;
|
|
public bool insideTrigger = false;
|
|
public bool carrying = false;
|
|
public GameObject carriableObject;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if(insideTrigger && Input.GetKeyDown(KeyCode.D) && !carrying)
|
|
{
|
|
carrying = true;
|
|
carriableObject.GetComponent<Rigidbody>().isKinematic = true;
|
|
}
|
|
else if(carrying && Input.GetKeyDown(KeyCode.D))
|
|
{
|
|
carrying = false;
|
|
carriableObject.GetComponent<Rigidbody>().isKinematic = false;
|
|
}
|
|
|
|
if(carrying)
|
|
{
|
|
carriableObject.transform.position = carryPosition.transform.position;
|
|
carriableObject.transform.rotation = carryPosition.transform.rotation;
|
|
}
|
|
}
|
|
|
|
void OnTriggerEnter(Collider other)
|
|
{
|
|
if(other.gameObject.tag == "PickupObject" && !carrying)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|