Help With Jumping!

So I’m making a game where you can press a button and flip gravity and my problem is I cant get the jump to work the same when gravity is flipped, hers the code.

public class BetterJump : MonoBehaviour
{
    public float FallMultiplier = 2.5f;
    public float LowJumpMultiplier = 2f;
    public float OtherFallMultiplier = 2.5f;
    public float OtherLowJumpMultiplier = 2f;


    Rigidbody2D rb;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        if(rb.velocity.y < 0 && rb.gravityScale == 4)
        {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (FallMultiplier - 1) * Time.deltaTime;
        }
        else if(rb.velocity.y > 0 && !Input.GetButton("Jump") && rb.gravityScale == 4)
        {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (LowJumpMultiplier - 1) * Time.deltaTime;
        }

        if (rb.velocity.y < 0 && rb.gravityScale == -4)
        {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (OtherFallMultiplier  - 1) * Time.deltaTime;
        }
        else if (rb.velocity.y > 0 && !Input.GetButton("Jump") && rb.gravityScale == -4)
        {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (OtherLowJumpMultiplier - 1) * Time.deltaTime;
        }
    }
}

Imo that code is a bit messy. First of all don’t use the RigidBody component inside Update(), use FixedUpdate(). Next thing i wouldn’t do is using the .gravityScale outside the range [-1, 1]. Instead use a variable to control the speed and just use .gravityScale to know the direction. Take also a look to the tutorials on the player movement because .velocity isn’t the best solution for the movement, for the jump it’s ok. Try to code clean as possible, of course not a list of “if”, try something like this:

Update(){
   direction = ... //Get the player input

   if ("Player press space bar"){
      Jump()
   }

   if("Player press gravity button){
      ChangeGravity()
   }
}

FixedUpdate(){
   Move()
}

void Move(){
   //Code to move based on the direction and the speed
}

void Jump(){
   //Code to jump
}
void ChangeGravity(){
   //Code to flip the gravity
}
1 Like

Oh ok thank you! Ya I’m pretty new to unity and I know I need to clean it up, but my main problem is that I can flip my character gravity with a button and I cant get the jump to feel the same when gravity is normal. I just don know how to do that.

Heres it before I messed with anything, I just need it to work when gravity is flipped in the opposite direction.

public float FallMultiplier = 2.5f;
    public float LowJumpMultiplier = 2f;


    Rigidbody2D rb;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void FixedUpdate()
    {
        if(rb.velocity.y < 0)
        {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (FallMultiplier - 1) * Time.deltaTime;
        }
        else if(rb.velocity.y > 0 && !Input.GetButton("Jump"))
        {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (LowJumpMultiplier - 1) * Time.deltaTime;
        }

    }
}

This is how i rotate the gravity in my game:

void RotatePlayerGravityUpsideDown()
{
Physics2D.gravity = new Vector2(0, 9.81f);
}

1 Like

Oh no I already am able to flip gravity, I just need the jump to feel the same as this when it is flipped as well.

Just try it out, it may affect it differently. If it doesnt work, then its 5 mintues of your time lost.

ok I will.

How would I code that?

Wherever you flip the gravity, just call that function.

1 Like

This is what it looks like now, im not exactly sure how to do that.

public class Gravity : MonoBehaviour
{
  
    private Rigidbody2D rb;
    private bool top;
    private PlayerController player;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        player = GetComponent<PlayerController>();
      
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (player.IsGrounded == true)
            {
                rb.gravityScale *= -1;
                Rotation();
            }
            else
                return;
        }

    }
    void Rotation()
    {
        if(top == false)
        {
            transform.eulerAngles = new Vector3(0, 0, 180f);
        }
        else
        {
            transform.eulerAngles = Vector3.zero;
        }
        player.FacingRight = !player.FacingRight;
        top = !top;
    }

  


}
void Rotation()
    {
        if(top == false)
        {
            transform.eulerAngles = new Vector3(0, 0, 180f);
            RotateGravity();
        }
        else
        {
            transform.eulerAngles = Vector3.zero;
            RotateGravityToNormal();
        }
        player.FacingRight = !player.FacingRight;
        top = !top;
    }

void RotateGravity()
{
    Physics2D.gravity = new Vector2(0, 9.81);
}

void RotateGravityToNormal()
{
    Physics2D.gravity = new Vector2(0.-9.81)
}

That should work, but did it very quickly so could be some typos. FYI, -9.81 is the force of gravity IRL and is used as Unity’s standard (as far as i remember). You can always change it from -9.81 or you can change the mass of the player, either works.

sadly no, its not working, player gets stuck on the roof and cant move.

public class Gravity : MonoBehaviour
{
   
    private Rigidbody2D rb;
    private bool top;
    private PlayerController player;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        player = GetComponent<PlayerController>();
       
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (player.IsGrounded == true)
            {
                rb.gravityScale *= -1;
                Rotation();
            }
            else
                return;
        }

    }
    void Rotation()
    {
      
       
            if (top == false)
            {
                transform.eulerAngles = new Vector3(0, 0, 180f);
                RotateGravity();
            }
            else
            {
                transform.eulerAngles = Vector3.zero;
                RotateGravityToNormal();
            }
            player.FacingRight = !player.FacingRight;
            top = !top;
       

        void RotateGravity()
        {
            Physics2D.gravity = new Vector2(0, 9.81f);
        }

        void RotateGravityToNormal()
        {
            Physics2D.gravity = new Vector2(0, -9.81f);
        }
    }

   


}

Thats because you will need 2 jump functions now:

public void DoJump()
    {
            rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
       
    }
          
    public void DoUpsideDownJump()
    {
            rb.AddForce(new Vector2(0, -jumpForce), ForceMode2D.Impulse);           
       
    }

Your jump force is still trying to go up (from original standpoint), and when the player is upsidedown, then they are just smushing into the ceiling.

oh no I mean it works and I can jump, but when I try and switch it back the player wont go down.

Did you get the negative sign for the RotateGravityToNormal function?

ya I did, I believe the issue is that it doesn’t work with my jump.