- Home /
Help scripting Combination lock/ pressure plates
OK to keep things simple, on my game level im going to have 5 pressure plates on the floor.
To keep things simple lets name them A,B,C,D,E.
I want to make the player press them in a certain order for a door to open or an animation play.
For example i want the pressure plates to be pressed in B,A,D,E,C order but i want them to reset if the user inputs them in the wrong order. What is the simplest way to do this?
Can anyone help me write the correct scripting.
Thanks in advance
Answer by WilsonS · May 09, 2011 at 05:40 PM
You are going to need two scripts. One to process individual pressure plates, and one to detect finished condition. As for requiring a specific order, just put the pressure plate objects into the control array in the desired order with the editors drag and drop.The following is some untested pseudo code to get you going in the right direction.
PressurePlate.cs ////////////////////////////////////////////////////////////
using UnityEngine; using System.Collections;
public class PressurePlate: MonoBehaviour { public bool isDown;
void Start(){ isDown = false; // default to not down }
void OnTriggerEnter( Collider other){ // only set as down if a game object named "Player" has collided with me if(other.name == "Player") isDown = true; }
} /////////////////////////////////////////////////////////
PressurePlateCtrl.cs /////////////////////////////////////////////////////////
using UnityEngine; using System.Collections;
public class PressurePlateCtrl :MonoBehaviour { public PressurePlate[] myPlates; public Animation myFinal; // door opening object animation
void Start(){ ResetPlates(); myFinal.Stop(); }
void ResetPlates (){ for(int itor =0; itor < myPlates.Length; itor++){ myPlates[itor].isDown = false; } }
void Update(){ // dont process if myFinal animation has already run or is not attached if(myFinal == null) return; bool swapFlag = true;
for(int itor = 0; itor < < myPlates.Length; itor++){
if(swapFlag){
if(myPlates[itor].isDown == false) swapFlag = false;
}else{
if(myPlates[itor].isDown == true){
Reset(); // true encountered after 1 false indicates out of order
}
}
}
// if swapFlag is true here, that means all plates are down in the correct order
if(swapFlag == true){
myFinal.Play();
myFinal = null; // break connection so we dont reprocess
}
} } ///////////////////////////////////////////////////////
Your answer
![](https://koobas.hobune.stream/wayback/20220613132926im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Pick up key and unlock a locked door 1 Answer
Door key code 2 Answers
Need help with my animation script 2 Answers
door script problems 2 Answers
Door script lag with rotation (Quaternion.slerp ?) 0 Answers