I want my if statement to run only if a certain state is running in my enum. I am brand new to them.
public enum Movement { Right, Left, IdleRight, IdleLeft, JumpRight, JumpLeft }
public Movement MovementStates;
if(Input.GetKeyDown(KeyCode.Space) && Movement.IdleRight))
{
MovementStates = Movement.JumpRight;
}
This is basically what I am trying to do but I do not know the syntax or if it is event possible.
I hope this explains what I am trying to do. I am totally new and just trying to learn as I go.
Just to be clear, I copied the top of my code and put it in there just so you can see what I am trying to call. This is not how my script looks.
I figured it out. Just in case someone can use this in the future here is what I did.
if(Input.GetKeyDown(KeyCode.Space) && MovementStates == Movement.IdleRight)
Kickapk:
I want my if statement to run only if a certain state is running in my enum. I am brand new to them.
public enum Movement { Right, Left, IdleRight, IdleLeft, JumpRight, JumpLeft }
public Movement MovementStates;
if(Input.GetKeyDown(KeyCode.Space) && Movement.IdleRight))
{
MovementStates = Movement.JumpRight;
}
This is basically what I am trying to do but I do not know the syntax or if it is event possible.
I hope this explains what I am trying to do. I am totally new and just trying to learn as I go.
A few tips;
public Movement MovementState
Your storing a single state of one object, be clear in your definition so its clear when you read your code.
If(Input.GetKeyDown(KeyCode.Space) && MovementState == Movement.IdleRight))
use instead
If(MovementState == Movement.IdleRight && Input.GetKeyDown(KeyCode.Space)))
&& logical AND requires both left and right conditions to be true. It always evaluates left first, and right only if left is true.
Input.GetKeyDown is a method call to a class, and Movement.IdleRight is a simple boolean comparison. The later is far less expensive to execute on a CPU.
Setting the order to least expensive to most expensive in && conditions means faster execution of code.