- Home /
Creating a Switch that Controls a Gate
I'm trying to make a script where if the player steps on the switch, a gate opens, and if he steps off, the gate closes.
I just finished learning some very basic scripting, and am unsure of how to go about writing this script with the limited scripting knowledge I have. Can someone direct me to what I need to learn scripting-wise to be able to write a script such as this?
Thanks.
Answer by devin8 · Dec 12, 2012 at 11:07 PM
You need a trigger.
You can create an empty gameobject and assign a collider from Component->Physics.
If you already have a object for switch then use it instead and assign collider on it. Make the collider dimensions large enough so that the player triggers it when he stands on the switch. Check the trigger flag on the collider in the inspector.
Then add a script on the gameobject that has a function like this
void OnTrigger(Collider other){
if(other.gameobject==<reference to your player object>)
Debug.Log("Write door logic here");
}
I typically keep reference to my player object in a static variable in a class named GameController.
Your answer
Follow this Question
Related Questions
"Destroy" bullet without turning off trail renderer 2 Answers
Network.Destroy and collision; destroying correct object. 1 Answer
Destroy() not working on collision 2 Answers
destroy instantiated game object on collision with another game object 1 Answer
Destroy temp object and make new object. 0 Answers