My animation doesn't stop when hitting colliders

Got a weird one for ya…

I run my player into a collider/ wall, and hold the movement key to continue running into the wall.

When I let go of the key, the animation continues to play for about 1 second.

Then a random frame from another walk cycle plays.

Then animation returns to normal.

Any clue what this could be caused by? I am at a loss!

If you keep the animator state machine open while doing this, you should be able to see what’s happening. There’s a good chance that your transitions aren’t quite right. Click on the arrows between states. In the inspector tab that opens up, try turning off “has exit time” for each transition.

Ok, I will check on this. Still learning the ropes, so I should have thought about that. Thanks! I’ll check back in on it.

Hm, so no luck there. I realized I had already turned off “has exit time.” I’m going to go ahead and copy my code here to see if it clears anything up:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Rewired;




public class BitchFaceController : MonoBehaviour
{

    public int playerId = 0;
    private Player player;
    public bool useController;

    public Animator topAnimator;
    public Animator bottomAnimator;
    public GameObject crossHair;

    public Rigidbody2D rb;

    public GameObject arrowPrefab;

  

    Vector3 movement;
    Vector3 aim;
    bool isAiming;
    bool endAiming;

    public int speed;

    private void Awake()
    {
        Debug.Log("PlayerId is " + playerId.ToString());
        player = ReInput.players.GetPlayer(playerId);

        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

   
   

    // Update is called once per frame
    void Update()
    {
        ProcessInputs();
        AimAndShoot();
        Animate();
    }

    private void FixedUpdate()
    {
        Move();
    }




    private void ProcessInputs()
    {
        if (useController)
        {
            movement = new Vector3(Input.GetAxis("MoveHorizontal"), Input.GetAxis("MoveVertical"), 0.0f);
            aim = new Vector3(player.GetAxis("AimHorizontal"), player.GetAxis("AimVertical"), 0.0f);
            aim.Normalize();
            isAiming = player.GetButton("Fire");
            endAiming = player.GetButtonUp("Fire");
        }
        else
        {
            movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0.0f);
            Vector3 mouseMovement = new Vector3(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), 0.0f);
            aim = aim + mouseMovement;
            if (aim.magnitude > 1.0f)
            {
                aim.Normalize();
            }
            isAiming = Input.GetButton("Fire1");
            endAiming = Input.GetButtonUp("Fire1");
        }

        if (movement.magnitude > 1.0f)
        {
            movement.Normalize();
        }

    }

    private void Move()
    {
        transform.position = transform.position + movement * speed * Time.deltaTime;
        rb.velocity = new Vector2(movement.x, movement.y);
    }

    private void Animate()
    {
        bottomAnimator.SetFloat("MoveHorizontal", movement.x);
        bottomAnimator.SetFloat("MoveVertical", movement.y);
        bottomAnimator.SetFloat("MoveMagnitude", movement.magnitude);

        topAnimator.SetFloat("MoveHorizontal", movement.x);
        topAnimator.SetFloat("MoveVertical", movement.y);
        topAnimator.SetFloat("MoveMagnitude", movement.magnitude);

        topAnimator.SetFloat("AimHorizontal", aim.x);
        topAnimator.SetFloat("AimVertical", aim.y);
        topAnimator.SetFloat("AimMagnitude", aim.magnitude);
        topAnimator.SetBool("Aim", isAiming);
    }


    private void AimAndShoot()
    {
        Vector2 shootingDirection = new Vector2(aim.x, aim.y);

        if (aim.magnitude > 0.0f)
        {
            crossHair.transform.localPosition = aim * 0.4f;
            crossHair.SetActive(true);

            shootingDirection.Normalize();
            if (endAiming)
            {
                GameObject arrow = Instantiate(arrowPrefab, transform.position, Quaternion.identity);
                arrow.GetComponent<Rigidbody2D>().velocity = shootingDirection * 3;
                arrow.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
                Destroy(arrow, 1.0f);
            }
        }
        else
        {
            crossHair.SetActive(false);
        }
    }

}

I don’t know this for sure, but setting all those animation parameters every frame could be a big performance hit. Since they’re all in reaction to “ProcessInputs,” I suggest you set the params there instead. That way, you can avoid setting them if nothing changed.

But that won’t fix your bug.

I don’t see anything in your code that would cause this problem. My guess is still the animation settings. See how, in my screenshot, the transition between animation states is a straight, vertical line? If there’s any overlap between your animation states, that could cause the behavior you’re seeing.

Also check all of your state transitions. Maybe two transitions share a condition. For example, if one happens when MoveHorizontal>.5, and another happens when MoveHorizontal>.6, you could end up with unpredictable behavior. Try to make the transitions mutually exclusive:

  • Transition1 happens if A and NOT B
  • Transition2 happens if B and NOT A
    That might help with your random animation flicker.