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 /
  • Help Room /
This question was closed Dec 03, 2016 at 01:05 PM by Hydropulse17 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Hydropulse17 · Dec 03, 2016 at 12:22 PM · physicsrigidbodyinputaddforcefixedupdate

GetAxis being missed in FixedUpdate work around?

I feel like I'm having a colossal brain fart right now, but I can't figure this out.

 void FixedUpdate()
     {
         moveHorizontal = Input.GetAxis("Horizontal");
         moveVertical = Input.GetAxis("Vertical");
         jump = Input.GetAxis("Jump");
 
         movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
 
         rb.AddForce(movement * speed);
 
         if (canJump)
         {
             rb.AddForce(0, jump * jumpHeight, 0);
         }
     }


Player movement works fine but jumping is buggy. If I apply the force to jump in FixedUpdate, it gets missed sometimes if the player is jumping a lot, and also, for some reason, if the player is moving diagonally(Like maybe GetAxis is being overloaded with three buttons being pressed?!).

But if I move it to Update, a. I'll have frame rate related issues in the build, right? And b. the jumping works perfect except every once in a while the player jumps much higher, which I believe is the result of the force being added twice. Maybe I'm interpreting it all wrong? I don't like using GetAxis or Unity's physics engine, there's too much going on under the hood.

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

  • Sort: 
avatar image
0
Best Answer

Answer by tanoshimi · Dec 03, 2016 at 12:33 PM

Two golden rules:

  • Always listen for input in Update.

  • Always apply physics functions in FixedUpdate.

To achieve this, all you need to do is separate your code:

 float jump;
 Vector3 movement;

 void Update() {
      float moveHorizontal = Input.GetAxis("Horizontal");
      float moveVertical = Input.GetAxis("Vertical");
      movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
      jump = Input.GetAxis("Jump");
 }

 void FixedUpdate() {
      rb.AddForce(movement * speed);
      if (canJump) {
          rb.AddForce(0, jump * jumpHeight, 0);
      }
  }
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 Hydropulse17 · Dec 03, 2016 at 12:57 PM 0
Share

Oh gosh, it seems so obvious now. It worked like a charm. Thank you.

avatar image Bunny83 · Dec 03, 2016 at 02:07 PM 0
Share

Actually this recommendation is not quite right. It's perfectly fine to read state data in FixedUpdate and should be read there. Otherwise you will have one frame delay since FixedUpdate is called before Update.

However one-time-events like button down / up events, mouse clicks always need to be handled in Update. You also can apply one-time-forced in Update. There's no need to route it to FixedUpdate.

Also jumps are usually one-time velocity boosts so using the normal AddForce doesn't make sense since it will divide the force by deltaTime since it's ment to be applied every frame. So for the jump button you usually would use Input.GetButtonDown("Jump") inside Update and not getting the virtual axis:

 void Update() {
     if (canJump && Input.GetButtonDown("Jump"))
     {
          rb.AddForce(0, jump * jumpHeight, 0, Force$$anonymous$$ode.Impulse);
     }
 }
 
 void FixedUpdate() {
     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");
     movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
     rb.AddForce(movement * speed * Time.deltaTime);
  }

Note: Force$$anonymous$$ode.Impulse applies a one time impulse that takes the objects mass into account. You could also use Force$$anonymous$$ode.Acceleration which ignores the mass of the object.

avatar image guavaman · Sep 08, 2018 at 06:57 PM 0
Share

I would add to Bunny83's comment that mouse axis values (deltas) and axis values that use the gravity/sensitivity system to ramp those values up/down over time should also not be queried in FixedUpdate.

Fixed Update can run multiple times per frame or zero times during a frame depending on the current frame rate and the set fixed update rate. Unity only updates its input system one time per frame, before either Fixed Update or Update run. Any value you obtain in Fixed Update is just going to be the exact same value Unity set when it updated its input system at the beginning of the frame. If Fixed Update runs 5 times during that frame, you'll get 5 copies of the same input value. While this doesn't affect GetButton calls or GetAxis calls for absolute axis values (joysticks), it does affect all other input values that you would want to be updated anew each frame. Unity did not design their input system to be used in Fixed Update.

Follow this Question

Answers Answers and Comments

99 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

Related Questions

Change direction of rigidbody when force is no longer being applied 2 Answers

Script makes plane point in general direction but doesn't point fully... read description please! 1 Answer

How to remove addforce effect 0 Answers

Physics-related touch input (Continuous) 0 Answers

How to handle FixedUpdate() and Input 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