- Home /
Need help with a pressure plate script (C#)(2D)
I want it so whenever I am not colliding with the pressure plate the boolean is false. This is probably easily solved but I am new to coding, still would appreciate the help. (It's a 2D game)
Here is my script :
 //Pressure plate variables
 public bool pressurePlate = false;
 
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     if (pressurePlate == true) {
         GetComponent<Animation>().Play ("pressurePlateDown");
     }
     if (pressurePlate == false) {
         GetComponent<Animation>().Play ("pressurePlateUp");
     }
 }
 void OnCollisionEnter2D(Collision2D other){
     if (other.transform.tag == "Player") {
         pressurePlate = true;
     }
 }
}
Answer by Pharaoh_ · Apr 09, 2015 at 06:25 PM
Is there any reason why you play the animation so frequently? Instead, I would use OnCollisionEnter2D() to set the value to true and OnCollisionExit2D to set it to false. In each of these functions, play the respective animation. If you want it to keep playing, then set it wrap mode in the Inspector to Looping.
Your answer
 
 
             Follow this Question
Related Questions
Error : Animator has not been initialized. UnityEngine.Animator:SetBool(String, Boolean) 1 Answer
How can i reverse all the booleans in a method? 2 Answers
How would I make 1 bool true whilst making others false 1 Answer
How to schedule responses (based on player's input text) for text adventure game? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                