Is there a way to determine if a player if still moving upwards or now going downwards while jumping

Hi guys.
i’m facing a problem here. I have a player which jumps when i press up arrow or w by adding an upward force to the players rigidbody2d.

The problem is, i have added a mushroom to the game where if the player collides with it (while the player is in a jumping animation state), the player is propelled upwards by adding again a force to its rigidbody.

This works when the player interacts with the mushroom while being pulled pack down by gravity, but when the player interacts with the mushroom while initiating the jumping phase, both the forces added when jumping and while interacting with the mushroom are added together.

Is there a way to make the mushroom only add force to the player only when they are coming back down, not going up,as in the mushroom ads force to the player when the player has a downward velocity, not upward velocity in their jumping state.

I’ll paste the players jumping code 1st and then the mushroom OnTriggerEnter2D code 2nd…

   if(Input.GetKey(KeyCode.UpArrow) && onGround || Input.GetKey(KeyCode.W) && onGround){
            anim.SetBool("standing",false);
            //anim.SetBool("running",false);
            anim.SetBool("jumping",true);
          rb.AddForce(transform.up * jumpForce * Time.deltaTime);
          
        }

void OnTriggerEnter2D (Collider2D col){
        if(col.gameObject.tag == "Player" && !playerMovementScript.onGround){
            // if(col.gameObject.GetComponent<Rigidbody2D>(). <= 0){

            // }
         col.gameObject.GetComponent<Rigidbody2D>().AddForce(transform.up * bounceForce * Time.deltaTime);
        }
    }

So if I understood right, you want to have the mushroom “jump effect” happen on the way down. I’m also assuming this script is on the ‘mushroom object’, but it seems like it should be on the playerMovement script for design purposes. So here’s how I would modify the playerMovement Script

Since you’re using rididbody physics, you can determine going down as if(rigidbody2D.velocity.y < 0). So you may modify the script to:

*1) check whether player has triggered a Mushroom object, and set a flag for it, bool isHitMushroom.

  1. during update do the comparison of the rigidbody2D.velocity.y, and perform jump there, if isHitMushroom.*

In your playerMovement Script:

private string s_Mushroom = "Mushroom"; //Or whatever your mushroom is tagged as
private RigidBody2D playerRigidBody2D;
private Bool isHitMushroom;

private void Awake ()
{
    playerRigidBody2D = GetComponent();
}

private void Update ()
{
    //All other Player Update Functions
    ....
    MushroomJumpEffect();
}

private void MushroomJumpEffect()
{
    if(isHitMushroom)
    {
        if (playerRigidBody2D.velocity.y < 0)
        {
            playerRigidBody2D = col.gameObject.GetComponent();
            playerRigidBody2D.AddForce(transform.up * bounceForce * Time.deltaTime);
            isHitMushroom = false;
        }
    }
}

private void OnTriggerEnter2D (Collider2D col)
{
    if (col.gameObject.CompareTag(s_Mushroom) && !onGround)
    {
        isHitMushroom = true;
    }
}