- Home /
Question by
jordanbrett50 · Jun 04, 2020 at 03:37 PM ·
scripting problemscript.scripting beginnerscriptingbasics
how to allow the key to only open 1 door rather than all of them?
I've been working on this script and I was wondering if anyone knew how to allow the key to only open 1 door rather than all of them as I am making a puzzle game.
Key Script: public class DoorKey: MonoBehaviour {
public bool inTrigger;
void OnTriggerEnter(Collider other)
{
inTrigger = true;
}
void OnTriggerExit(Collider other)
{
inTrigger = false;
}
void Update()
{
if (inTrigger)
{
if (Input.GetKeyDown(KeyCode.E))
{
DoorScript.doorKey = true;
Destroy(this.gameObject);
}
}
}
void OnGUI()
{
if (inTrigger)
{
GUI.Box(new Rect(0, 60, 200, 25), "Press E to take key");
}
}
}
Door Script:
public class DoorScript : MonoBehaviour {
public static bool doorKey;
public bool open;
public bool close;
public bool inTrigger;
void OnTriggerEnter(Collider other)
{
inTrigger = true;
}
void OnTriggerExit(Collider other)
{
inTrigger = false;
}
void Update()
{
if (inTrigger)
{
if (close)
{
if (doorKey)
{
if (Input.GetKeyDown(KeyCode.E))
{
open = true;
close = false;
}
}
}
else
{
if (Input.GetKeyDown(KeyCode.E))
{
close = true;
open = false;
}
}
}
if (open)
{
var newRot = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0.0f, -90.0f, 0.0f), Time.deltaTime * 200);
transform.rotation = newRot;
}
else
{
var newRot = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0.0f, 0.0f, 0.0f), Time.deltaTime * 200);
transform.rotation = newRot;
}
}
void OnGUI()
{
if (inTrigger)
{
if (open)
{
GUI.Box(new Rect(0, 0, 200, 25), "Press E to close");
}
else
{
if (doorKey)
{
GUI.Box(new Rect(0, 0, 200, 25), "Press E to open");
}
else
{
GUI.Box(new Rect(0, 0, 200, 25), "Need a key!");
}
}
}
}
}
Comment