- Home /
if condition not working
public class canonControl : MonoBehaviour {
bool deployed;
void Awake() {
deployed = false;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if((Input.GetButtonUp("Canon")) && deployed == false) {
animation.Play("canon_deploy");
deployed = true;
}
if ((Input.GetButtonUp("Canon")) && deployed == true) {
animation.Play("canon_undeploy");
deployed = false;
}
}
for a reason it is not working, it always play undeploy animation. Can any one please help me.
Comment
Best Answer
Answer by numberkruncher · Apr 25, 2013 at 02:52 PM
This is because both if statements are true in sequence since you change the value of deployed. Try the following instead:
bool deployed;
void Update() {
if (Input.GetButtonUp("Canon")) {
// Toggle state of deployment.
deployed = !deployed;
// Play desired animation.
if (deployed)
animation.Play("canon_deploy");
else
animation.Play("canon_undeploy");
}
}
Answer by whydoidoit · Apr 25, 2013 at 02:51 PM
Stick an else before the second if
At the moment your code will immediately meet the second condition if it met the first condition (read it and see!)
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How do i change speed of 2D animation 1 Answer
How to get the Quaternion Rotation from a Matrix4x4 2 Answers