Change gravity / Flip position?

I’m trying to achieve a jump much like the one they have in the game “VVVVVV”, whereas when you press space, the player flies toward the roof, and if you press again - the floor.

    public float speedForce = 50f;
    public Vector2 jumpVector;
    public Vector2 jumpVector2;
    bool isGroundedTop;
    bool isGroundedBot;

    public Transform grounderTop;
    public Transform grounderBot;
    public float radiuss;
    public LayerMask groundTop;
    public LayerMask groundBot;

    void FixedUpdate()
    {
        //When the player collides, change speedforce

        isGroundedTop = Physics2D.OverlapCircle(grounderTop.transform.position, radiuss);
        isGroundedBot = Physics2D.OverlapCircle(grounderBot.transform.position, radiuss);

        if (Input.GetKeyDown("space") && isGroundedTop == true)
        {
            isGroundedBot = false;
            GetComponent<Rigidbody2D>().AddForce(jumpVector * 150, ForceMode2D.Force);
        }
        if (Input.GetKeyDown("space") && isGroundedBot == true)
        {
            isGroundedTop = false;
            GetComponent<Rigidbody2D>().AddForce(jumpVector2 * 150, ForceMode2D.Force);

        }
    }

This is my current script. And yes, I am very well aware it’s stupid to create two of everything, I just wanted to try get a quick preview of how it works. With this script, the player can jump up, but is not able to jump down again. I’m sure there’s a very logical explanation for this, but right now I’m unable to see what. Any ideas what I should use to create a non-accelerating movement up and down?

what are jumpVector and jumpVector2 set to?

and aren’t the resets of the isGroundedBot/isGroundedTop backwards in the ifs?

Seems like you could do this pretty easy by just flipping gravity.

Something like this would do that

Physics.gravity *= -1;

//or in 2D

Physics2D.gravity *= -1;

Hamsterbyte, that is JUST the thing I was out after. It’s so motivating seeing how easily it can be achieved, but also extremely demotivating seeing how far off I actually am. Thank you!

I actually used a very similar mechanic in a game of mine on google play; ‘two lines’. There’s a link in my signature. Keep at it bro, you’ll get the hang of this yet! Good on ya for not losing the motivation when you hit a wall.

1 Like