- Home /
Multi Switch Door
hi, im fairly new to unity i currently creating a basic fps project with a simple two way door system i.e a switch that destroys its assigned object im just wondering if theirs any possible way to make it so that the player has to activate multiple switches in order to open the door or destroy it im not looking for coding per say however im just looking to be pushed in the right direction. any help will be greatly appreciated.
Answer by You! · Jun 04, 2012 at 03:39 PM
You need to create a script (attached to the door) with as many public booleans as there are switches (or, if you rather, an public integer originally set to zero). Each time a switch is activated, change one of the bools to true (or add one to the integer) using the formula [scriptname].[varname] in a separate script. Lastly, use an if statement to check to see that all of the bools are true (or that the integer is equal to the number of switches). Then, either destroy the door ("Destroy(gameObject)") or activate an animation for the door (Animation.Play("/name of animation/")).
Answer by ryand444 · Jun 04, 2012 at 03:44 PM
thank you very much appreciated do you know of any tutorials or guides which could help under stand the process further since your explanation was good however i need some examples to help me put it into my context and i apologies if this is asking to much.
Attach this script to a door :
// Door_Opener_Script
public var Button1 : Button_Script;
public var Button2 : Button_Script;
function Update()
{
if (Button1.IsPressed && Button2.IsPressed)
{
// Open the Door
Destroy (gameObject);
}
}
On 2 buttons, attach this script to each :
// Button_Script
public var IsPressed : boolean = false;
private var timer : float = 0.0;
private var timerReset : float = 3.0;
function OnTriggerEnter( otherCol : Collider )
{
IsPressed = true;
timer = timerReset;
}
function Update()
{
if (timer < 0)
{
IsPressed = false;
timer = 0;
}
else
{
timer -= Time.deltaTime;
}
}
Then drag-and-drop each button into the Door Inspector (Button1 & Button2)
When anything activates the trigger on a button, the button is set to true. (i have included a timer, so the button starts counting down, then resets to false). The script on the door reads each button script and finds out if IsPressed is set to true. If so, open the door. This is just a quick example and not the best explaination, but let me know if you have any qns on this idea =]
EDIT : I have put a Destroy (gameObject); call where you asked. But you could do many things at this line, play an animation, disable a rigidbody, etc.
Here is the Unity Script Reference for Object.Destroy : http://unity3d.com/support/documentation/ScriptReference/Object.Destroy.html
and OnTriggerEnter : http://unity3d.com/support/documentation/ScriptReference/Collider.OnTriggerEnter.html
thank you very much this is very helpful i will contact you if i have any more questions however this is exactly what i was looking for once again thanks
" i do have one more question how would i go about destroying the door after all the switches have been activated as there is a { open door } in the script you provided but i just wouldnt know how to wright the object being destroyed "
I have edited my comment.
Answer by ryand444 · Jun 04, 2012 at 06:40 PM
i do have one more question how would i go about destroying the door after all the switches have been activated as there is a { open door } in the script you provided but i just wouldnt know how to wright the object being destroyed
Answer by ryand444 · Jun 07, 2012 at 08:32 PM
i ma sorry to bother once again but i would like to know if there is any way of showing the time limit maybe as a GUI to the player once again i don't expect a solid answer a budge in the right direction would be helpful. thanks
This should really really really really be a comment. Please convert it to one so that we know who you are talking to!
Your answer
Follow this Question
Related Questions
Sliding door animation question. 3 Answers
Go through door, by loading next scene, with key-press 1 Answer
I am trying to make a door, when using this script nothing happens. 0 Answers
Opening door with the same key? 1 Answer
Sliding Door question 2 Answers