When facing left, P1’s forward or backward animation keep looping although button released and stop moving. Appreciate if you can advise how to stop the looping animation, thanks.
A. Speed of Backward and Forward from both Layers are 1.
B. Both layers sharing same forward and backward animation
C. Transitions:
Conditions| Base |Flip
idle > forward = forward | true | false
forward > idle = forward |false | true
Conditions| Base |Flip
idle > backward = Backward | true | false
backward > idle = Backward |false | true
private Animator Anim;
[SerializeField] public float WalkSpeed = 0.05f;
private bool IsJumping = false;
private AnimatorStateInfo Player1Layer0;
private bool CanWalkLeft = true;
private bool CanWalkRight = true;
public GameObject Player1;
public GameObject Opponent;
private Vector3 OppPosition;
private bool FacingLeft = false;
private bool FacingRight = true;
// Start is called before the first frame update
void Start()
{
Anim = GetComponentInChildren<Animator>();
StartCoroutine(FaceRight());
}
// Update is called once per frame
void Update()
{
// listen to the animator
Player1Layer0 = Anim.GetCurrentAnimatorStateInfo(0);
// Screen border set up (X axis)
Vector3 ScreenBounds = Camera.main.WorldToScreenPoint(this.transform.position);
if(ScreenBounds.x > Screen.width - 70)
{
CanWalkRight = false;
}
if (ScreenBounds.x < 50)
{
CanWalkLeft = false;
}
else if (ScreenBounds.x > 50 && ScreenBounds.x < Screen.width - 70)
{
CanWalkRight = true;
CanWalkLeft = true;
}
//get the opponent's position
OppPosition = Opponent.transform.position;
//facing left or right of the Opponent
if(OppPosition.x > Player1.transform.position.x)
{
StartCoroutine(FaceLeft());
}
if (OppPosition.x < Player1.transform.position.x)
{
StartCoroutine(FaceRight());
}
// Walking left and right
if (Player1Layer0.IsTag("Motion"))
{
if (Input.GetAxis("Horizontal") > 0)
{
if (CanWalkRight == true)
{
Anim.SetBool("forward", true);
transform.Translate(WalkSpeed, 0, 0);
}
}
if (Input.GetAxis("Horizontal") < 0)
{
if (CanWalkLeft == true)
{
Anim.SetBool("backward", true);
transform.Translate(-WalkSpeed, 0, 0);
}
}
}
if (Input.GetAxis("Horizontal") == 0)
{
Anim.SetBool("forward", false);
Anim.SetBool("backward", false);
}
// Jumping once and bending
if (Input.GetAxis("Vertical") > 0)
{
if (IsJumping == false)
{
IsJumping = true;
Anim.SetTrigger("jump");
StartCoroutine(jumpPaused());
}
}
if (Input.GetAxis("Vertical") < 0)
{
Anim.SetBool("bend", true);
}
if (Input.GetAxis("Vertical") == 0)
{
Anim.SetBool("bend", false);
}
}
IEnumerator jumpPaused()
{
yield return new WaitForSeconds(.5f);
IsJumping = false;
}
IEnumerator FaceLeft()
{
if (FacingLeft == true)
{
FacingLeft = false;
FacingRight = true;
yield return new WaitForSeconds(0.15f);
Player1.transform.Rotate(0, -180, 0);
Anim.SetLayerWeight(1, 0);
}
}
IEnumerator FaceRight()
{
if (FacingRight == true)
{
FacingRight = false;
FacingLeft = true;
yield return new WaitForSeconds(0.15f);
Player1.transform.Rotate(0, 180, 0);
Anim.SetLayerWeight(1, 1);
}
}
}