- Home /
Flip between 2 states constantly C#?
I have created 2 walking animations, one left leg step, and one right leg step. I want it to where if I hold W or the Up key, it will be in Walk mode. While in walk mode, it should flip between Left Step, and once Left Step finishes, play Right step, and loop until W isn't pressed. I wouldn't need a whole script, just an example showing the flipping between 2. Any help will be appreciated!
Thanks in advance!
If you combine the two, you can just play the full walk cycle on loop
Answer by Stratosome · Aug 15, 2018 at 03:57 AM
Hi there!
Well, there are a number of ways to flip between two states. It largely depends on the specifics of the behavior you are wanting. You can create a super simple, two way 'state machine' using a boolean.
bool isLightOn = false;
// Swap states
isLightOn = !isLightOn;
For your example, where it is left step, right step, left step, and so on, you could potentially do something slightly more advanced like this:
public enum WalkingStateEnum { LeftStep, RightStep };
private readonly float ANIM_TIME = 2.5f;
public WalkingStateEnum walkingState;
public bool isWalking = false;
private void Update() {
if (Input.GetKeyDown(KeyCode.W)) {
isWalking = true; // -------------- Is now walking
StartCoroutine(LeftStep()); // ---- Starts walking with left step
}
if (Input.GetKeyUp(KeyCode.W)) {
isWalking = false; // ------------- Stopped walking
}
}
private IEnumerator LeftStep() {
walkingState = WalkingStateEnum.LeftStep;
yield return new WaitForSeconds(ANIM_TIME);
// If still walking, start right step
if (isWalking) {
StartCoroutine(RightStep());
}
}
private IEnumerator RightStep() {
walkingState = WalkingStateEnum.LeftStep;
yield return new WaitForSeconds(ANIM_TIME);
// If still walking, start left step
if (isWalking) {
StartCoroutine(LeftStep());
}
}
Now, this code bit certainly isn't perfect, but it gets the idea across. You could use Enums for states (which I like to do) and flip those enum values to whatever when changing states. You would probably want to interupt the coroutines using maybe StopCoroutine() if you are in fact using coroutines. If you are using Unity's Animator and such though, and not your own script animations or whatever you want to call them, you might want it done a different way. If you are using Unity's Animator, you could insert events into your animations saying something like if it is still walking, start the next animation, else just stop.
If having distinct states for left step and right step are important, these are possible ways to go about it. However, if you are going for just a basic walk animation, there may be better ways of doing it? I dunno. Hard to say without more details, but hopefully this is somewhat helpful!
Your answer
Follow this Question
Related Questions
how to keep track of Lean Tween function? 0 Answers
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i check if animation has finished playing if the object have no animator attached ? 1 Answer
UNITY 2D: How to make a level selection menu (like the video in details)? 1 Answer
Help with animator controllers 1 Answer