Hello!
First time posting, I hope it’s formatted properly!
To start off I have only been using unity for a month so I am very new to scripting, I am using Unity version 2022.3.47f1 and Visual Studio 17.11.3 .
I am making a prototype for an escape room game, I need the player to be able to pickup coloured keys and unlock their respective locked door with animations (WORKING) and the player needs to pickup/move/drop items (WORKING), but I’m not understanding the programming for interacting with regular/non-locked doors and drawers with LMB…
using UnityEngine;
using System;
public class PickupController : MonoBehaviour
{
[Header("Pickup Settings")]
[SerializeField] public Transform holdArea;
private GameObject heldObject;
private Rigidbody heldObjectRB;
[Header("Physics Parameters")]
[SerializeField] private float pickupRange = 5.0f; // Pickup range is 5.
[SerializeField] private float pickupForce = 150.0f; // Pickup force is 150.
[Header("Zoom Settings")]
[Space]
private bool pickedUp;
private bool positionSet;
public LayerMask layerMask;
private void Update()
{
if(Input.GetMouseButtonDown(0)) // Check if input is Mouse 0 down (LMB click), if it is...
{
if(heldObject == null) // ... Then check if the held object is equal to null, in other words, if nothing is being held, then...
{
RaycastHit hitnew; // ... Raycast hit (object detection)...
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hitnew, pickupRange)) // ...If Raycast detects object...
{
PickupObject(hitnew.transform.gameObject); // Pickup the object!
}
//if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hitnew, pickupRange, layerMask)) // ...If Raycast detects object...
//{
// PickupObject(hitnew.transform.gameObject); // Pickup the object
}
else // If the above conditions are not met...
{
DropObject(); // ... Then drop the object.
}
}
if(heldObject != null) // But if held object does not equal to null/ nothing (in other words, if object is being held)...
{
MoveObject(); // ... Then move object.
}
//this is the framework of detecting interaction.
//WE want to ensure that the ray is detecting what we look at- NOT JSUT THE CAMERA
// Once the ray is not detecting the camera. we can then add the if(input.getkeydown) to the script as its detecting the correct item.
RaycastHit hit;
if (Physics.Raycast(Camera.main.transform.position, Vector3.forward * 10, out hit, layerMask))
{
Debug.Log(hit.transform.gameObject.name);
if (Input.GetMouseButtonDown(0))
{
Draw draw = hit.transform.gameObject.GetComponent<Draw>();
draw.isOpen = true;
}
else
{
}
}
if(pickedUp)
{
Vector3 newPos = new Vector3(heldObject.transform.position.x, heldObject.transform.position.y, Mathf.Clamp(heldObject.transform.position.z, -0.18f ,01f));
heldObject.transform.Translate(Camera.main.transform.forward * Time.deltaTime * 100 * Input.GetAxis("Mouse ScrollWheel"), Space.World); // Enables the scroll wheel to zoom held items towards and away from the player.
heldObjectRB.constraints= RigidbodyConstraints.FreezeRotation; // When picked up, object totation is froze.
}
else // If above conditions are not met...
{
return; // ...Try again.
}
}
void MoveObject()
{
if(Vector3.Distance(heldObject.transform.position, holdArea.position) > 0.1f) // Checking distance between the object being held and the hold area, checking it is greater than 0.1f.
{
Vector3 moveDirection = (holdArea.position - heldObject.transform.position); // Where the held object is going to move to: Held area MINUS the object position = how far object is going to need to move.
heldObjectRB.AddForce(moveDirection * pickupForce); // Object is static, so moveDirection causes it to be moved.
}
}
void PickupObject(GameObject pickObj)
{
if(pickObj.GetComponent<Rigidbody>()) // Checks if the object has a rigidbody, if not the object can't be held.
{
pickedUp = true;
heldObjectRB = pickObj.GetComponent<Rigidbody>(); // Adds the object as the players held item
heldObjectRB.useGravity = false; // Objects original gravity is now false so it will not fall down while being held.
heldObjectRB.drag = 10; // Drag force is 10.
heldObjectRB.constraints = RigidbodyConstraints.FreezeRotation; // Once picked up, object rotation is froze.
heldObjectRB.constraints = RigidbodyConstraints.FreezePosition; // Once picked up, object position is froze.
heldObjectRB.isKinematic = true; // When item is picked up, it is kinematic.
heldObjectRB.transform.parent = holdArea; // Want to parent picked up object to held area.
heldObject = pickObj; // Makes the held object attach to the pickup point.
}
}
void DropObject()
{
pickedUp = false;
heldObjectRB.useGravity = true; // Held object gets its own gravity back.
heldObjectRB.drag = 1; // Held object gets its drag back to 1 (original).
heldObjectRB.constraints = RigidbodyConstraints.None; // No more rigidbody constraints.
heldObjectRB.isKinematic = false; // When item is dropped, it is not kinematic.
heldObject.transform.parent = null; // Held object is no longer a child object of the pickup holder.
heldObject = null; // Object not being held.
}
}
The above is the programming for my ‘PickupController’ for pickup/move/dropping objects, this is working. Lines 53 to 70 is the framework for the opening of regular/non-locked doors and drawers
The person that is helping me with programming wanted to put the door/ drawer opening and closing in the same syntax. I guess another question is should this be separate?
Please let me know if you require more info or anything! Thank you!![]()
v v v
ETA for clarity
Here is a screenshot of the game, there are objects that can be picked up (boxes, books, colour coded keys…), but I need to make it so the (unlocked) drawers in the desk for instance, or a wardrobe door can be opened with LMB.
The programming above works for picking up, moving, and dropping objects, and another script I have that works to open locked doors, BUT I cannot seem to get programming to open an unlocked door, lol
I’ll attach my programming ‘Doors’ for unlocking doors with keys, which does work!
using UnityEngine;
public class Doors : MonoBehaviour
{
public Animator animator;
public string doorType;
// Start is called before the first frame update
void Start()
{
animator = GetComponentInParent<Animator>(); // Get animator component from child objects.
}
public void OnTriggerEnter(Collider collision) // When collision is detected in a certaion area, then do something if conditions are met.
{
if (collision.gameObject.tag == "Green Key" && doorType == "green") // If there is collision of green key and green door...
{
Debug.Log("Green Lock Opened"); // ...Then debug in console...
animator.SetBool("open", true); // ...And open!
Destroy(collision.gameObject);
}
else
{
if (collision.gameObject.tag == "Blue Key" && doorType == "blue") // If there is collision of blue key and green door...
{
Debug.Log("Blue Lock Opened"); // ...Then debug in console...
animator.SetBool("open", true); // ...And open!
}
else
{
if (collision.gameObject.tag == "Red Key" && doorType == "red") // If there is collision of red key and red door...
{
Debug.Log("Red Lock Opened"); // ...Then debug in console...
animator.SetBool("open", true); // ...And open!
}
else
{
return;
}
}
}
}
I was initially wanting to put the regular drawer/ door interactions here, but was told it should be under PickupController
Again, thank you!!
