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 commodore · Jun 20, 2012 at 03:31 AM · updatefixedupdate

Not sure if this should go in Update or FixedUpdate function

Here's the code that makes my character jump. When the jump button is released, the character is supposed to jump. In the docs, it says to put "GetButtonUp" in the Update function and to put anything that deals with physics in the FixedUpdate function. The problem is I have both in my little bit of code.

 // jump code
 if (Input.GetButtonUp ("Jump"))
 {
     rigidbody.velocity = rigidbody.velocity + Vector3.up * jumpAmount;
 }
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
7
Best Answer

Answer by Eric5h5 · Jun 20, 2012 at 04:28 AM

It should be in Update, because GetButtonUp only returns true for the single frame that it happens in, and FixedUpdate doesn't necessarily run every frame, so it can and does miss ButtonDown/Up events.

Just to clarify a bit more, if you're applying forces continuously that should be in FixedUpdate, but with a one-off application of force, it doesn't really matter if it's in Update or FixedUpdate.

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 Fattie · Jun 20, 2012 at 07:46 AM 0
Share

Flawlessly stated down to the last comma !!!

avatar image commodore · Jun 20, 2012 at 02:29 PM 0
Share

Perfect answer thanks! And yeah my character was only jumping sometimes when it was in FixedUpdate.

avatar image reptilebeats · Jun 20, 2012 at 06:55 PM 0
Share

i always get confused with this as my code to press a button does something physically constantly, i always tend to use fixed for stuff like that and update for menus.

avatar image
-1

Answer by crtapps · Jun 20, 2012 at 04:02 AM

The FixedUpdate is framerate-independent, you can modify the variable you wanna update in the way of framerate-independent in Update by multiply the variable by Time.deltaTime.

Comment
Add comment · Show 7 · 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 commodore · Jun 20, 2012 at 04:24 AM 0
Share

Where exactly should I add Time.deltaTime? I did this and his momentum just completely stops. Thanks for replying

 // jump code
 if (Input.GetButtonUp ("Jump"))
 {
     rigidbody.velocity = rigidbody.velocity * Time.deltaTime + Vector3.up * jumpAmount;
 }
avatar image Eric5h5 · Jun 20, 2012 at 04:29 AM 0
Share

You shouldn't use Time.deltaTime in this case.

avatar image crtapps · Jun 20, 2012 at 04:36 AM 0
Share

I think you should make the jumpAmout multiply by the Time.deltaTime:

rigidbody.velocity = rigidbody.velocity + Vector3.up jumpAmount Time.deltaTime;

BTW, If you just want your character to jump up, you dont need to use physical. There is an example in Unity's resource page:

http://unity3d.com/support/resources/example-projects/character-animation

You can take a look at the Goober example in the CharacterAnimation, the script named Super$$anonymous$$arioController.js.

avatar image Eric5h5 · Jun 20, 2012 at 04:44 AM 0
Share

I think you should make the jumpAmout multiply by the Time.deltaTime:

No, you really shouldn't. Unless you want broken code that doesn't work right. Remember, it's a one-time event, not continuous.

avatar image crtapps · Jun 20, 2012 at 04:54 AM 0
Share

Yes, Eric is right. the velocity dont need to multiply by deltaTime, sry for the mistake.

Show more comments

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Eliminating input loss 1 Answer

how to achieve a LateFixedUpdate effect 5 Answers

FixedUpdate performance doubts 2 Answers

Splitting one event's code between Update and fixedUpdate 2 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