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
0
Question by hardband · Feb 01, 2015 at 11:12 PM · c#2d2d-physicsjumping

Character jumping at random heights. What in my code is causing this?

Hey, does anyone know why this code is causing my character to jump at varying heights? It isn't proportional to how long the button is held and seems random.

 public class DodgeManController : MonoBehaviour {
 
     public float maxSpeed = 10.0f;
     public float jumpForce = 5.0f;
     bool grounded;
     public float groundRadius = 0.2f;
     public Transform groundCheck;
     public LayerMask whatIsGround;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update(){
         if (grounded && Input.GetAxis ("Jump") > 0) {
             rigidbody2D.AddForce (Vector2.up * jumpForce,ForceMode2D.Impulse);
         }
     }
 
     void FixedUpdate () {
 
         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
 
         float move = Input.GetAxis ("Horizontal");
 
         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
 
     }
 }


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
Best Answer

Answer by Alex_May · Feb 01, 2015 at 11:19 PM

Put your AddForce code in FixedUpdate. Since you can't guarantee how often Update() is called, your AddForce code could be getting called multiple times or not at all between physics updates. Anything that interacts with the physics system should be put in FixedUpdate, which is guaranteed to be called at regular intervals.

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 hardband · Feb 01, 2015 at 11:29 PM 0
Share

It still seems to be varying the jump height every now and again, although it seems less extreme now.

avatar image Alex_May · Feb 01, 2015 at 11:37 PM 0
Share

Poll the input in Update(), rather than FixedUpdate(), and store the result in a bool. Then you can test that bool in FixedUpdate, and set it to false after processing it. That way your input will only get used once. What's happening is that since FixedUpdate might (or might not) run multiple times in between Updates, and Input is only updated when Update happens (rather than for FixedUpdate), your AddForce code is being run multiple times.

avatar image hardband · Feb 01, 2015 at 11:50 PM 0
Share

Thanks, I've managed to fix it. Thanks for all the help.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make a Grid movement (tile per tile) 1 Answer

Even / Constant Speed and Jumping 2D 1 Answer

Debug a rigidbody2d velocity on an exact point 1 Answer

Collider just for Tag (2D) (C#) 1 Answer

Problem with disabled collision in 2D 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