Jumping bug

So after learning beginner scripting in Unity, I tried making my own game which would be a box platform game in which the player (who would be controlling a box) would have to move through obstacles in order to reach the finish line. One of the abilities that I’m making for the player is a jump feature. At first, I coded a simple movement system in which the player could only move left and right and jump.

the initial script was something like this

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;
    public float force;
    public float jumpForce;
    //float doubleJumpCount = 0;
    bool doubleJump;
    bool jump;
    bool left;
    bool right;
    // Update is called once per frame
    void Update()
    {
        jump = Input.GetButtonDown("Up");
        left = Input.GetButton("Left");
        right = Input.GetButton("Right");
        /*
        if(jump)
        {
            doubleJumpCount++;
            Debug.Log("doubleJumpCount = " + doubleJumpCount);
            if(doubleJumpCount == 3)
            {
                jump = false;
                doubleJumpCount = 0;
            }
        }*/
    }
    void FixedUpdate()
    {
        if(right)
        {
            rb.AddForce(Vector3.right * force * Time.deltaTime);
        }
        if(left)
        {
            rb.AddForce(Vector3.left * force * Time.deltaTime);
        }
        if(jump)
        {
            rb.AddForce(Vector3.up * jumpForce * Time.deltaTime);
        }
    }
}

Upon testing, I found out that I could just spam the w-key (I set ‘w’ as the key for “Up” in the project settings) and the player could make the box flying in the air. Additionally, the box could fall over the platforms in the level because of the physics. To avoid both of these problems, I decided to limit the player to double jumping only and allow the player to move in the forward and backward direction too. I added a few lines of code into my script and this is what I have now:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;
    public float force;
    public float jumpForce;
    float doubleJumpCount = 0;
    bool doubleJump;
    bool jump;
    bool forward;
    bool backwards;
    bool left;
    bool right;
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.name == "Floor")
        {
            doubleJumpCount = 0;
        }
    }
    // Update is called once per frame
    void Update()
    {
        jump = Input.GetButtonDown("Jump");
        left = Input.GetButton("Left");
        right = Input.GetButton("Right");
        forward = Input.GetButton("Up");
        backwards = Input.GetButton("Down");
        if (jump)
        {
            doubleJumpCount++;
            Debug.Log("doubleJumpCount = " + doubleJumpCount);
            if (doubleJumpCount > 2) jump = false;
        }
    }
    void FixedUpdate()
    {
        if (right) rb.AddForce(Vector3.right * force * Time.deltaTime);
        if (left) rb.AddForce(Vector3.left * force * Time.deltaTime);
        if (forward) rb.AddForce(Vector3.forward * force * Time.deltaTime);
        if (backwards) rb.AddForce(Vector3.back * force * Time.deltaTime);
        if (jump)
        {
            rb.AddForce(Vector3.up * jumpForce * Time.deltaTime);
        }
    }
}

The double jump mechanic works. However, if the box is stationary and I press the jump button only (in this case I set it as “space”), the box would jump to the direction of the positive or negative axis of x. Sometimes, after double jumping once, it would just slide towards the negative axis of x whenever I press space again. The effect I wanted it to produce is to move vertically up towards the positive side of the y-axis only. What could be the problem here?

I should also note that I tried changing the if(jump) conditional in the FixedUpdate() function into these two versions:

if(jump)
        {
            rb.AddForce(new Vector3(0,1,0) * jumpForce * Time.deltaTime);
        }

and

if(jump)
        {
            rb.AddForce(new Vector3(1,1,0) * jumpForce * Time.deltaTime);
        }

the values I have set in the script component are:
Rb: Player (Rigid Body)
Force: 1000
Jump Force: 10000

Also, I inserted a physics material to my platforms which is set to 0 static friction and 0 dynamic friction.

I think in the rigid body component there are things like isKinematic and also freeze rotation (or something like that not sure of the exact wording) i think if you would set it to kinematic you would have to simulate gravity manualy by constantly applying downward force, but there would be no tilting.Also try to freeze those rotations one at a time to see whats going on… don’t quote me on this though : - )

Just an experiment:
if you change the jump code to this:

if (jump)
        {
            rb.velocity = Vector3.zero;
            rb.AddForce(Vector3.up * jumpForce * Time.deltaTime);
        }

Does the problem go away?

No, but as I looked at the debug log it seems that my doubleJumpCount does not reset to 0 every time the player object hits the floor object and doubleJumpCount just keeps on increasing.

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

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;

    public float force;
    public float jumpForce;

    float doubleJumpCount = 0;

    bool doubleJump;

    bool jump;
    bool forward;
    bool backwards;
    bool left;
    bool right;

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.collider.name == "Floor")
        {
            doubleJumpCount = 0;
        }
    }

    // Update is called once per frame
    void Update()
    {
        jump = Input.GetButtonDown("Jump");
        left = Input.GetButton("Left");
        right = Input.GetButton("Right");
        forward = Input.GetButton("Up");
        backwards = Input.GetButton("Down");




       
        if(jump)
        {
            doubleJumpCount++;

            Debug.Log("doubleJumpCount = " + doubleJumpCount);
            if (doubleJumpCount > 2) jump = false;
           
        }
    }

    void FixedUpdate()
    {
        if(right) rb.AddForce(Vector3.right * force * Time.deltaTime);
        if(left) rb.AddForce(Vector3.left * force * Time.deltaTime);
        if (forward) rb.AddForce(Vector3.forward * force * Time.deltaTime);
        if (backwards) rb.AddForce(Vector3.back * force * Time.deltaTime);
        /*if(jump)
        {
            rb.AddForce(Vector3.up * jumpForce * Time.deltaTime);
        }*/
        if (jump)
        {
            rb.velocity = Vector3.zero;
            rb.AddForce(Vector3.up * jumpForce * Time.deltaTime);
        }
    }
}

I think the problem has something to do with the jump boolean value

4937213--479120--doubleJumpBug.png

that’s because the Jump bool is true only for one frame ( the " Input.GetButtonDown " frame )so it doesnt matter if you set it to false after 2 jumps because its false anyway.
i think you are better off doing someting like this

if (Input.GetButtonDown("Jump")) {
            Jump();
        }

and put your logic into that Jump function.
like so

public void Jump()
    {
        if (doubleJumpCount > 2)
            return;
        else
        {
            rb.velocity = Vector3.zero;
            rb.AddForce(Vector3.up * jumpForce * Time.deltaTime);
            doubleJumpCount++;
        }
    }

Ok that fixed the multiple jump issue, but there is still a bug where whenever I press the jump button there is a force that is pushing the box to the negative direction of the x-axis

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

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;

    [SerializeField] private float force;
    [SerializeField] public float jumpForce;

    float doubleJumpCount = 0;

    bool doubleJump;

    bool jump;
    bool forward;
    bool backwards;
    bool left;
    bool right;

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.collider.name == "Floor")
        {
            doubleJumpCount = 0;
        }
    }

    // Update is called once per frame
    void Update()
    {
        jump = Input.GetButtonDown("Jump");
        left = Input.GetButton("Left");
        right = Input.GetButton("Right");
        forward = Input.GetButton("Up");
        backwards = Input.GetButton("Down");




       
        if(jump)
        {
            //doubleJumpCount++;

            Debug.Log("doubleJumpCount = " + doubleJumpCount);
            //if (doubleJumpCount > 2) jump = false;
           
        }
    }

    void FixedUpdate()
    {
        if(right) rb.AddForce(Vector3.right * force * Time.deltaTime);
        if(left) rb.AddForce(Vector3.left * force * Time.deltaTime);
        if (forward) rb.AddForce(Vector3.forward * force * Time.deltaTime);
        if (backwards) rb.AddForce(Vector3.back * force * Time.deltaTime);
        /*if(jump)
        {
            rb.AddForce(Vector3.up * jumpForce * Time.deltaTime);
        }*/
        if (jump)
        {
            Jump();
        }
    }

    void Jump()
    {
        if(doubleJumpCount >= 2)
        {
            return;
        }
        else
        {
            rb.velocity = Vector3.zero;
            rb.AddForce(Vector3.up * jumpForce * Time.deltaTime);
            doubleJumpCount++;
        }
    }
}

Could the settings in the project settings for the jump button be related to the problem?

note: sorry for the late reply, I have been busy for the last 2 weeks.

4965095--483173--jump bug.JPG