'How can I make it so when my player collides with an enemy, it disables movement input until player touches the ground?

So, I want to make it so the player loses control when it collides with an enemy. I already got a thing set up to make the player fly off towards the direction of the collision, but I can't seem to be able to make them lose control. I want input to not register.

Here's my player controller script:

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public Rigidbody2D _rigidBody2d;
    Animator animatorController;

    public float moveSpeed;
    public float jumpForce = 1f;
    float maxJumpHeight;

    bool isOnGround = false;
    bool collidedWithEnemy = false;

    private void Start()
    {
        _rigidBody2d = GetComponent<Rigidbody2D>();
        animatorController = GetComponent<Animator>();
    }

    float horizontalInput;

    private void Update()
    {
        if (collidedWithEnemy == false)
        {
            horizontalInput = Input.GetAxis("Horizontal");
        }

        // Player movement
        MovementMechanics();
        // Sprite Flipping
        SpriteFlipping();
        // Jumping
        JumpingMechanics();
        // Set Falling Animation
        FallingMechanics();
    }

    void MovementMechanics()
    {
        if (horizontalInput != 0)
        {
            transform.Translate(new Vector2(1, 0) * horizontalInput * Time.deltaTime * moveSpeed);
            animatorController.SetBool("isMoving", true);
        }
        else
        {
            animatorController.SetBool("isMoving", false);
        }
    }

    void SpriteFlipping()
    {
        if (horizontalInput > 0)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
        else if (horizontalInput < 0)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
    }

    void JumpingMechanics()
    {
        if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
        {
            _rigidBody2d.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
            isOnGround = false;

            animatorController.SetBool("isMoving", false);
            animatorController.SetBool("isJumping", true);
            animatorController.SetBool("isFalling", false);
        }
    }

    void FallingMechanics()
    {
        if (_rigidBody2d.velocity.y < 0f)
        {
            animatorController.SetBool("isFalling", true);
        }
        else if (_rigidBody2d.velocity.y > 0f)
        {
            animatorController.SetBool("isFalling", false);
            animatorController.SetBool("isJumping", true);
        }
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        // Avoids Jump Spawn
        if (other.gameObject.CompareTag("Ground"))
        {
            isOnGround = true;
            collidedWithEnemy = false;

            animatorController.SetBool("isJumping", false);
            animatorController.SetBool("isMoving", false);
            animatorController.SetBool("isFalling", false);
        }

        // On Collision With Enemy
        if (other.gameObject.CompareTag("Enemy"))
        {
            collidedWithEnemy = true;
        }
    }
}

And here's the enemy script:

using UnityEngine;

public class EnemyController : MonoBehaviour
{
    Vector2 otherTransform;
    Rigidbody2D playerRigidBody2d;

    public float impulseForce = 100f;

    private void Start()
    {
        playerRigidBody2d = GameObject.Find("Player").GetComponent<Rigidbody2D>();
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            otherTransform = other.gameObject.transform.position;
            playerRigidBody2d.AddForce((otherTransform * 1) * impulseForce, ForceMode2D.Impulse);
            other.transform.Translate(Vector3.zero);
        }
    }
}

And here's a little video of whats happening: https://vimeo.com/709461296

As you can see in the console, the game still registers horizontal input after collision with enemy

(Don't mind the player launching off at lightspeed, thats not my concern here, I can fix that easily)



Solution 1:[1]

I'd say the problem is that you don't set horizontalInput to zero if the collision happens. The way you wrote your Update method, it takes the previously assigned value of horizontalInput and processes it.

Solution 2:[2]

Add the condition of not colliding with the enemy in MovementMechanics():

if (horizontalInput != 0 && !collidedWithEnemy)
{
    // do something..
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adok
Solution 2 KiynL