Hello!
I’ve been working on my first Mobile Game and I’m using animations to make my car change lanes. It’s working perfectly until I stumbled upon this unexplainable problem. Basically I tell the animation to play depending on the keycode pressed and the last animation played. I can change lanes from right then left but when I change lanes to the left one I can’t go back to the middle lane. I’ll post some screenshots. I don’t know what the problem is as the code seems to be correct. Thank you!
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayAnimation : MonoBehaviour
{
private Animator animator;
private bool isAnimatingLeft = false;
private bool isAnimatingRight = false;
private bool isAnimatingRightLeft = false;
private bool isAnimatingLeftRight = false;
private bool isIdle = true;
private void Start()
{
animator = GetComponent<Animator>();
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.LeftArrow) && isIdle)
{
animator.SetBool("isIdle", false);
isIdle = false;
animator.SetBool("isAnimatingLeft", true);
isAnimatingLeft = true;
}
if (Input.GetKeyDown(KeyCode.LeftArrow) && isAnimatingRightLeft)
{
animator.SetBool("isAnimatingRightLeft", false);
isAnimatingRightLeft = false;
animator.SetBool("isAnimatingLeft", true);
isAnimatingLeft = true;
}
if (Input.GetKeyDown(KeyCode.RightArrow) && isIdle)
{
animator.SetBool("isIdle", false);
isIdle = false;
animator.SetBool("isAnimatingRight", true);
isAnimatingRight = true;
}
if (Input.GetKeyDown(KeyCode.LeftArrow) && isAnimatingRight)
{
animator.SetBool("isAnimatingRight", false);
isAnimatingRight = false;
animator.SetBool("isAnimatingRightLeft", true);
isAnimatingRightLeft = true;
}
if (Input.GetKeyDown(KeyCode.RightArrow) && isAnimatingLeft)
{
animator.SetBool("isAnimatingLeft", false);
isAnimatingLeft = false;
animator.SetBool("isAnimatingLeftRight", true);
isAnimatingLeftRight = true;
}
if (Input.GetKeyDown(KeyCode.RightArrow) && isAnimatingRightLeft)
{
animator.SetBool("isAnimatingRightLeft", false);
isAnimatingRightLeft = false;
animator.SetBool("isAnimatingRight", true);
isAnimatingRight = true;
}
if (Input.GetKeyDown(KeyCode.RightArrow) && isAnimatingLeftRight)
{
animator.SetBool("isAnimatingLeftRight", false);
isAnimatingLeftRight = false;
animator.SetBool("isAnimatingRight", true);
isAnimatingRight = true;
}
if (Input.GetKeyDown(KeyCode.LeftArrow) && isAnimatingLeftRight)
{
animator.SetBool("isAnimatingLeftRight", false);
isAnimatingLeftRight = false;
animator.SetBool("isAnimatingLeft", true);
isAnimatingLeft = true;
}
}
}