x


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

more ▼

asked May 09 '11 at 03:18 PM

Christoff gravatar image

Christoff
82 19 19 27

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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
      }  
   }
}
///////////////////////////////////////////////////////
more ▼

answered May 09 '11 at 05:40 PM

WilsonS gravatar image

WilsonS
52 1 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3418
x200
x129
x4
x1

asked: May 09 '11 at 03:18 PM

Seen: 738 times

Last Updated: May 09 '11 at 03:18 PM