Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kropcke · Jul 05, 2019 at 04:00 PM · collisionrigidbody2dgravity

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kropcke · Jul 07, 2019 at 06:39 AM

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;
         }
     }
 
 
 }
 

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

166 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rigidbody2D Individual Gravity 1 Answer

How do I make a rigidbody2d that doesn't do anything on collision? 1 Answer

,My Character is falling through my Tilemap 0 Answers

Error when colliding and calling a IEnumerator 1 Answer

Overlapping dynamic Rigidbody2D objects 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges