- Home /
This question was
closed May 10, 2013 at 09:02 PM by
Fattie for the following reason:
Duplicate Question
Door opening with a key, my scripts and problem
So I have my door open script which is working great:
var Range : boolean = false;
var ThePlayer : Transform;
var sound : AudioClip;
var sound2 : AudioClip;
var DoorClosed : boolean = true;
var text : GUIText; // it shows on screen "Press mouse button to open/close the door"
function Start ()
{
text.enabled = false;
}
function Update ()
{
var distance : float = Vector3.Distance (ThePlayer.transform.position, transform.position);
if (distance < 3f)
{
Range = true;
}
else
{
Range = false;
}
if (Range == true)
{
text.enabled = true;
if(Range == true && Input.GetMouseButtonDown(0) && DoorClosed == true)
{
animation.Play ("dooropen");
DoorClosed = false;
audio.PlayOneShot(sound);
}
else if (Range == true && Input.GetMouseButtonDown(0) && DoorClosed == false)
{
animation.Play ("doorclose");
DoorClosed = true;
audio.PlayOneShot(sound2);
}
}
else if (Range == false)
{
text.enabled = false;
}
}
And my key script which works too:
var Range : boolean = false;
var ThePlayer : Transform;
var text2 : GUIText;
var GotKey : boolean = false;
function Start ()
{
text2.enabled = false;
}
function Update ()
{
var distance : float = Vector3.Distance (ThePlayer.transform.position, transform.position);
if (distance < 3f)
{
Range = true;
}
else
{
Range = false;
}
if(Range == true)
{
text2.enabled = true;
}
if(Range == true && Input.GetMouseButtonDown(0))
{
animation.Play ("key");
GotKey = true;
}
else if(Range == false)
{
text2.enabled = false;
}
}
And my question is.. how to add needed key to a door script.
Comment
Best Answer
Answer by roojerry · May 10, 2013 at 07:46 PM
You already have the GotKey boolean in you Key script. I would give the door a reference to the key object needed to open the door and just use the GotKey boolen in the door opening logic.
something like:
if(Range == true && Input.GetMouseButtonDown(0) && DoorClosed == true && key.GotKey)