- Home /
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;
}
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.
Perfect answer thanks! And yeah my character was only jumping sometimes when it was in FixedUpdate.
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.
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.
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;
}
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.
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.
Yes, Eric is right. the velocity dont need to multiply by deltaTime, sry for the mistake.
Your answer
Follow this Question
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