[2D] Can't AddForce to x but y works fine.

Hey guys. Noob problems incoming :smiley:

So I’m working on this 2D Game and wanted to add something like a sliding-movement.
Played Titanfall 2 recently and yeah… ^^

I’m stuck at the basic functionality. I want to AddForce to my RigidBody2D Character on the x axis by GetButton.

The function looks basicaly the same like my Jump() function. Only thing is that I switched the variables position to x and set y to 0:

Jump() Function(everything works here. the character gets the force correctly.):

   void Jump()
    {
        if (isGrounded)
        {
            canJumpAgain = true;
            animator.SetTrigger("Jump");
            rigidBody.velocity = new Vector2(rigidBody.velocity.x, 0);
            rigidBody.AddForce(new Vector2(0, jumpSpeed), ForceMode2D.Impulse);
        }
        else if(!isGrounded)
        {
            if (canJumpAgain)
            {
                animator.SetTrigger("DoubleJump");
                rigidBody.velocity = new Vector2(rigidBody.velocity.x, 0);
                rigidBody.AddForce(new Vector2(0, jumpSpeed), ForceMode2D.Impulse);
                canJumpAgain = false;
            }
        }
    }

Kick() Function(I don’t get any force from anywhere.):

    void Kick()
    {
        if (isGrounded)
        {
            rigidBody.AddForce(new Vector2(jumpSpeed, 0), ForceMode2D.Impulse);
            attackController.currentDamage = attackController.normalKickDamage; 
            animator.SetTrigger("Kick");
            attackController.isKicking = true;
        }
        else if (!isGrounded)
        {
            animator.SetTrigger("JumpKick");
            attackController.currentDamage = attackController.critKickDamage;
            attackController.isKicking = true;
        }
    }

Here again the difference between the
AddForce on Jump():

rigidBody.AddForce(new Vector2(0, jumpSpeed), ForceMode2D.Impulse);

and AddForce on Kick():

rigidBody.AddForce(new Vector2(jumpSpeed, 0), ForceMode2D.Impulse);

I really don’t get it.

My GameObject Hierarchy:

  • Player1 (the parent containing the
    previous scripts, Rigidbody2D and a
    Collider2D for isGrounded purposes)
  • -Skin (has an animator controlling the child sprites)
  • –Head
  • –Body (contains collider set as a trigger for incoming damage)
  • –RightArm
  • –LeftArm (contains collider set as a trigger for outgoing damage)
  • –RightFoot
  • –LeftFoot

what could cause a hurdle that don’t let my player1 game object move to the side?

isGrounded works fine so no problem here.

Fixed it by removing any changes in velocity I made in code.

“In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour.”

-Unity Documentation