Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
-1
Question by Eydamson · Mar 05, 2015 at 05:45 AM · unity5rigidbody2dphysics2djumping

Unity5 rigidbody2d not working properly... :(

I have a playercontroller script that handles the gravity and jumping of the player.. but what happens is the player is bearly jumping and falling. But my script is fine with unity4, after i upgrade my game on unity5 the problem occurs... please help.. There is a function applygravity there and jump function you can ctrl+f to search, sorry for the mess :)

note: no other script is handling my physics for the player except this playercontroller script

by the way my game is like flappy bird style...

===================================================================================================

 /// <summary>
 /// the logic of the gameloop of this class
 /// </summary>
 /// <returns></returns>
 IEnumerator OnUpdate()
 {
     //start catching  if the user taps the screen
     StartCoroutine("CheckTap");

     //do this only if the player is not dead
     while (true)
     {
         switch (playerState)
         {
             case PlayerState.Idle:
                 break;
             case PlayerState.Flying:
                 //when the player is not on the ground
                 if (!groundCheck.IsGrounded || GameController.Instance.debugMode)
                 {
                     //do the calcualtions on the air
                     DoAirLogic();

                     //dont let the player go up offscreen 
                     RestrictVerticalPosition();
                 }
                 break;
             case PlayerState.Dead:
                 //stop checking if the player is dead
                 StopCoroutine("CheckTap");
                 playerAnimator.PauseAnimation();
                 DidTap = false;
             if (groundCheck.IsGrounded && !isDead && rigid2D.velocity == Vector2.zero)
                 {
                     isDead = true;
                     //playerState = PlayerState.Idle;
                     StartCoroutine("DeathState");
                 }

                 break;
             default:
                 break;
         }
  
         //apply gravity
         ApplyGravity();
         //wait for the fixed update
         yield return new WaitForFixedUpdate();
     }
 }

   

 #region player air and ground logic calculation
 /// <summary>
 /// this is where the calulations or logics when the player is not on the ground
 /// </summary>
 void DoAirLogic()
 {

     //when the user taps the screen
     if (DidTap)
     {
         DidTap = false;
         //cast a ray from the touch points on the screen to test if we tap on a powerups
         RaycastHit2D touchHit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

         //if the user tap on a powerup
         if (touchHit.collider != null)
         {
             if (touchHit.collider.gameObject.tag == "powerup")
             {
                 //get the powerups from the container and use it
                 PowerUpContainer pu = touchHit.collider.gameObject.GetComponent<PowerUpContainer>();
                 pu.ApplyMyPower();
             }
         }

         //makes the player jump
         Jump();
     }

     //rotate the player  up wars if the player is going up or downwards if the player is giong down but only if he is grounded
     //if (!groundCheck.IsGrounded)
       //  DoRotation();
 }

 /// <summary>
 /// Apply the gravity only if it is enabled
 /// </summary>
 void ApplyGravity()
 {
     //if we can use gravity
     if (EnableGravity)
     {
         //apply gravity only when we are not on the ground
         rigid2D.AddForce(new Vector2(0.0f, -gravity));
     }
 }

 /// <summary>
 /// This makes player jump
 /// </summary>
 void Jump()
 {
     //play the jump sound
     GetComponent<JumpSound>().PlayOnce();

     //make a jumpfore vector based on the jumprate set on the inspector
     Vector2 jumpFoce = new Vector2(rigid2D.velocity.x, jumpRate);

     //reset the current velocity of the player
     //we do not want to add up this jumpforce velocity to the current velocity of the player
     rigid2D.velocity = Vector2.zero;

     //apply the velocity to our rigidbody
     //this makes the player to jump
     rigid2D.AddForce(jumpFoce,ForceMode2D.Force);
 }
 #endregion
 
 
Comment
Add comment · Show 4
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
avatar image hexagonius · Mar 05, 2015 at 06:48 AM 0
Share

Too much code. Break it down to what u think is the part causing problems. Answers is not about code debugging you know

avatar image Eydamson · Mar 05, 2015 at 06:51 AM 0
Share

sorry :( ill change it right away...

avatar image Anonimys · Mar 05, 2015 at 11:17 AM 1
Share

Same problem, I convert my Unity4 Project to Unity5 and i noticed my code

 hit.GetComponent<Rigidbody2D>().gravityScale

And

 hit.GetComponent<Rigidbody2D>().AddForce

Do not work correctly in Unity5...

avatar image Eydamson · Mar 06, 2015 at 12:17 AM 0
Share

yes, i've been searching it in days but i havent find any solotion...

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by GiyomuGames · Mar 05, 2015 at 11:02 AM

Do you have an animation on the player? If go to its animator and uncheck "apply root motion". It corrected the similar problem I had.

Comment
Add comment · Show 3 · 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
avatar image Baintastic · Mar 05, 2015 at 09:54 PM 0
Share

Thanks! I was freaking out because I didn't back up my game before the conversion and this was the only problem :)

avatar image Eydamson · Mar 06, 2015 at 12:33 AM 0
Share

yes it works! thanks man you really save my day :)

avatar image Anonimys · Mar 07, 2015 at 05:54 PM 0
Share

yes thank you, this is one of the possible solutions

Link: Solution

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

23 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

Related Questions

player jumps too high when clicking with two fingers. 2 Answers

Unity 5: RigidBody or No RigidBody? 1 Answer

Random 2D Jumping With rigidbody2D.AddForce 1 Answer

Why is enemy jumping at different speed even though the velocity is the same? 0 Answers

RigidBody2D.MoveRotation() Custom Pivot 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