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 blenderblender · Jun 07, 2016 at 05:33 PM · 2dphysicsrigidbody2dgrounded

(2D) character jumping problem [is grounded detection, rigidbody2d]

Hello. There you can see my script. I try to make a 2D Jump and Run game. But sometimes if i hold the space button (for jumping) my character makes a unwanted "mega jump". I think because my character is grounded too long and the script adds the force to the 2d rigidbody two or more times. Can someone help me ?

 using UnityEngine;
 using System.Collections;
 
 public class CharacterController : MonoBehaviour {
     private Rigidbody2D rb;
       public float JumpForce = 1000;
     public bool isGroundet = false;
     public bool Jump = false;
     public Transform [] groundCheck;
     public LayerMask whatIsGround;
   
      
      // Use this for initialization
     void Start () {
         rb = gameObject.GetComponent<Rigidbody2D>();
        }
 
   
 
         void FixedUpdate () {
         
         rb.velocity = new Vector2(Speed, rb.velocity.y);
 
         if (Input.touchCount > 0 || Input.GetKey(KeyCode.Space) || Input.GetMouseButtonDown(0))
         {      
                Jump = true; 
         }
 
           isGroundet = false;
         
         
         if (Physics2D.OverlapCircle(groundCheck[0].position, .15f, whatIsGround))
           {
             isGroundet = true;
         }
 
             if (Jump && isGroundet&&rb.velocity.y<JumpForce)
              {
             rb.AddForce(Vector2.up * JumpForce);
           
             Jump = false;
         }   
           
         
             
     }
         
 
       
 }
 
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

2 Replies

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

Answer by Masterio · Jun 07, 2016 at 07:30 PM

Try to reset a valocity before:

rb.velocity = new Vector2(rb.velocity.x, 0f);

and then add the force:

rb.AddForce(Vector2.up * JumpForce);

Or simplify all of it by:

rb.velocity = new Vector2(rb.velocity.x, JumpForce);

Comment
Add comment · Show 1 · 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 blenderblender · Jun 09, 2016 at 04:04 PM 0
Share

Thank you :) It has worked.

avatar image
1

Answer by Eno-Khaon · Jun 07, 2016 at 10:34 PM

For best results, it's handy to keep in mind that inputs are registered during Update(). This means that OnGUI(), with double the framerate of Update(), will register inputs twice per frame and FixedUpdate(), not matching the timing of Update(), may occasionally miss inputs.

With that in mind, my example will break input and usage into two sections:

 // Updated touch input to be based on when the touch is first applied
 // Similarly, updated keyboard input to a GetKeyDown check
 void Update()
 {
     if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
     {      
         Jump = true; 
     }
 }
 
 void FixedUpdate()
 {
     // Multiplied velocity check by 0.95 for safety
     if (Jump && isGroundet && rb.velocity.y < JumpForce * 0.95f)
     {
         // Force applied is immediate.
         rb.AddForce(Vector2.up * JumpForce, ForceMode2D.Impulse);
         
         Jump = false;
     }
 }

To note, you would also want to divide your current "JumpForce" by about 50 (default physics step) in order to bring the jumping velocity back down to where you'd want it.

Edit: Whoops, right. Rigidbody2D.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutine not working properly 1 Answer

Rotate towards velocity (2D) 2 Answers

2d grounded check problem? 3 Answers

2D Geometry dash-like ship physics 0 Answers

How can I move a 2D GameObject left/right relative to its forward direction? 1 Answer


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