Jumping and reverse gravity with a Rigidbody2D (based on the Ruby tutorials)

Hi Everyone!

First, thank you to the developers for making the awesome 2D Ruby Adventure tutorials (all 14+ hours of them!).

After completing it, I went back over the first few, and began modifying the player mechanics in the Rubycontroller script to create a 2d platformer that employed jump and reverse gravity. The jumping and reverse gravity in my new script worked great - not a problem at all… until I tried to get rid of the jittering upon collision. I understand that there is a conflict between a scripted transform moving the character through a collider, and the Rigidbody2D component pushing the player back out. I’m just having trouble discovering which part(s) of my script needs to be fixed (I’m going to assume there are several).

Here’s my main problem: The four lines of code that I’ve commented out do fix the jittering, but now the player doesn’t jump, and falls very slowly. I’m discovering this is a pretty common problem, but I just can’t wrap my head around the solution.

Here’s the original script:

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

public class CharController : MonoBehaviour
{

    public float jumpHeight = 5f;
    public float UpsideDownJumpHeight = -5f;
    public static bool isJumping = false;
    public static bool isUpsideDown = false;

    //////////////Rigidbody2D rb2D;

    void Start()
    {
    //////////////rb2D = GetComponent<Rigidbody2D>();
    }

    void Update()
    {


        if (Input.GetButtonDown("Jump") && (isJumping == false))
        {
            gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpHeight), ForceMode2D.Impulse);
        }

        if (Input.GetButtonDown("Jump") && (isUpsideDown == true))
        {
            gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, UpsideDownJumpHeight), ForceMode2D.Impulse);
        }

        if (Input.GetKeyDown("f"))
        {
            GetComponent<Rigidbody2D>().gravityScale *= -1;
            transform.Rotate(Vector3.forward * 180);
        }

        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        Vector2 position = transform.position;
        //////////////Vector2 position = rb2D.position; <<-- REPLACES LINE ABOVE

        position.x = position.x + 3.0f * horizontal * Time.deltaTime;

        transform.position = position;
        //////////////rb2D.MovePosition(position); <<-- REPLACES LINE ABOVE

    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.tag == "Ground")
        {
            isJumping = false;
        }

        if (collision.collider.tag == "Ceiling")
        {
            isUpsideDown = true;
        }

    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.collider.tag == "Ground")
        {
            isJumping = true;
        }

        if (collision.collider.tag == "Ceiling")
        {
            isUpsideDown = false;
        }
    }

}

Thank you so much for any help you can provide - I really, really appreciate it. And if it makes any difference, I’m running 2018.4. Thanks again!

-kropcke

Fixed.

Thanks.

-kropcke

Here’s the code snippet:

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

public class CharController_new : MonoBehaviour
{

    public float jumpHeight = 5f;
    public float UpsideDownJumpHeight = -5f;
    public static bool isJumping = false;
    public static bool isUpsideDown = false;
    Rigidbody2D rb2D;

    void Start()
    {
        rb2D = GetComponent<Rigidbody2D>();
    }

    void Update()
    {

        float horizontal = Input.GetAxis("Horizontal");
        Vector2 position = rb2D.position;
        position.x = position.x + 3.0f * horizontal * Time.deltaTime;
        transform.position = position;

        if (Input.GetButtonDown("Jump") && (isJumping == false))
        {
            gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpHeight), ForceMode2D.Impulse);

        }

        if (Input.GetButtonDown("Jump") && (isUpsideDown == true))
        {
            gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, UpsideDownJumpHeight), ForceMode2D.Impulse);
        }

        if (Input.GetKeyDown("f"))
        {
            GetComponent<Rigidbody2D>().gravityScale *= -1;
            transform.Rotate(Vector3.forward * 180);
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.tag == "Ground")
        {
            isJumping = false;
        }

        if (collision.collider.tag == "Ceiling")
        {
            isUpsideDown = true;
        }

    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.collider.tag == "Ground")
        {
            isJumping = true;
        }

        if (collision.collider.tag == "Ceiling")
        {
            isUpsideDown = false;
        }
    }


}