Player Continues Towards Key Pressed When Movement Is Stopped

So basically what is happening is that when my player collides with another game object trigger collider, I stop all movement and play a death animation. Although it keeps moving in the direction of whichever key is pressed and I don’t know why. Here is my player script.

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

public class Player : MonoBehaviour
{
    [SerializeField] private float runSpeed = 5f;
    [SerializeField] private float jumpSpeed = 5f;
    [SerializeField] GameObject fireCast;
    [SerializeField] Collider2D myBodyCollider;
    [SerializeField] Collider2D myFeet;

    Rigidbody2D rb;
    Animator myAnimator;
    Collider2D myCollider2D;

    public float inputX;
    public bool isAlive = true;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();
        myCollider2D = GetComponent<Collider2D>();
    }

    private void Update()
    {
        if (!isAlive)
        {
            return;
        }
        Movement();
        AttackSword();
        BlockingSword();
        Casting();
    }

    private void Movement()
    {
        Walk();
        FlipSprite();
        Jump();
        Crouch();
    }

    private void Walk()
    {
        if (Input.GetAxisRaw("Horizontal") != 0)
        {
            inputX = Input.GetAxisRaw("Horizontal");
        }

        float controlThrow = Input.GetAxisRaw("Horizontal");
        Vector2 playerVelocity = new Vector2(controlThrow * runSpeed, rb.velocity.y);
        rb.velocity = playerVelocity;

        bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
        myAnimator.SetBool("Walking", playerHasHorizontalSpeed);
    }

    private void FlipSprite()
    {
        bool playerHasHorizontalSpeed = Mathf.Abs(rb.velocity.x) > Mathf.Epsilon;
        if (playerHasHorizontalSpeed)
        {
            transform.localScale = new Vector2(Mathf.Sign(rb.velocity.x), 1f);
        }
    }

    private void Jump()
    {
        if (!myFeet.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
            return;
        }

        if (Input.GetButtonDown("Jump"))
        {
            Vector2 jumpVelocityToAdd = new Vector2(0f, jumpSpeed);
            rb.velocity += jumpVelocityToAdd;
        }

        bool playerHasVerticalSpeed = Mathf.Abs(rb.velocity.y) > Mathf.Epsilon;
        myAnimator.SetBool("Jumping", playerHasVerticalSpeed);
    }

    private void Crouch()
    {
        if (!myCollider2D.IsTouchingLayers(LayerMask.GetMask("Ground")))
        {
            return;
        }

        if (Input.GetKey(KeyCode.LeftControl))
        {
            myAnimator.SetBool("Crouching", true);
        }
       
        else
        {
            myAnimator.SetBool("Crouching", false);
        }
    }
   
    private void AttackSword()
    {
        if (Input.GetButtonDown("Fire1") && !Input.GetKey(KeyCode.LeftShift))
        {
            myAnimator.SetTrigger("AttackingSword");
        }
    }

    private void BlockingSword()
    {
        if (Input.GetButtonDown("Fire2"))
        {
            myAnimator.SetTrigger("BlockingSword");
        }
    }

    private void Casting()
    {
        if (Input.GetKey(KeyCode.LeftShift) && Input.GetButtonDown("Fire1"))
        {
            myAnimator.SetTrigger("Casting");
            GameObject f = Instantiate(fireCast, transform.position, Quaternion.identity);
            if (inputX < 0)
            {
                f.GetComponent<SpriteRenderer>().flipX = true;
            }

            f.GetComponent<Casting>().targetDir = new Vector2(inputX, 0f);
        }
    }

    void Death()
    {
        myAnimator.SetTrigger("Death");
        isAlive = false;
        Destroy(gameObject, 1f);
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.CompareTag("Pickup") && collision.GetComponent<GroundItem>() != null)
        {
            Debug.Log("In range of pickup.");
            if (Input.GetKeyDown(KeyCode.E))
            {
                collision.GetComponent<GroundItem>().OnPickup();
            }
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Enemy"))
        {
            Death();
        }
    }

    public Vector3 GetPosition()
    {
        return transform.position;
    }

    public void DamageKnockback(Vector3 knockbackDir, float knockbackDistance, int damageAmount)
    {
        transform.position += knockbackDir * knockbackDistance;
        HeartsHealthVisual.heartsHealthSystemStatic.Damage(damageAmount);
    }

    public void Heal(int healAmount)
    {
        HeartsHealthVisual.heartsHealthSystemStatic.Heal(healAmount);

    }
    // Update controls
}

If anyone can help me tackle this problem it would greatly be appreciated. Thank you!

Your movement is based on physics velocity. You aren’t making any change to velocity when the player dies, so will continue with its current velocity and only change based on other collision, drag, or friction effects.

You should be making physics changes in FixedUpdate instead of Update by the way.

Thanks, this fixed it!

1 Like