Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 sidiusdoxidius · Oct 27, 2021 at 03:54 PM · unity 2dupdatevelocityfixedupdate

physics randomness problem

Hello i have a problem. When i press W the object jumps up but each time with a different height. I tried to do it through fixedupdate, but then he either jumps every time with a different height or does not jump at all.

  void Update()
         {
             Jump();
         }
     
     private void Jump()
         {
             if (Input.GetKeyDown(KeyCode.W))
             {
                 
                 var jumpVel = new Vector2(0, jumpForce * Time.deltaTime);
                 rb.velocity += jumpVel;
                 
             }
         }




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

Answer by Eno-Khaon · Oct 27, 2021 at 06:45 PM

You're basically scaling your jumping force erratically.

Constant forces/motions (i.e. velocity) never need to be scaled by the framerate. That's what you do when you're making that change yourself.

 // When you first change this, expect it to be ~60+ times
 // stronger than you intend, depending on your average framerate
 Vector2 jumpVel = new Vector2(0, jumpForce);
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
avatar image
0

Answer by rh_galaxy · Oct 27, 2021 at 04:30 PM

Something like this should do it.

 private bool bJumping = false;
 void FixedUpdate()
 {
     if (Input.GetKey(KeyCode.W) && !bJumping)
     {
         bJumping = true;
         Jump();
     } else bJumping = false; //now jumping is possible again
     //issue: you need to keep it true until you land (or jumping in the air is possible)
 }
      
 private void Jump()
 {
     var jumpVel = new Vector2(0, jumpForce * Time.fixedDeltaTime);
     rb.velocity += jumpVel;
 }

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 Eno-Khaon · Oct 27, 2021 at 06:46 PM 0
Share

Whenever possible (pretty much always), it's better to *NOT* check input in FixedUpdate() (where it's unreliable) and ensure it remains in Update() (where it is).

avatar image rh_galaxy Eno-Khaon · Oct 27, 2021 at 11:22 PM 0
Share

That's not true when using Input.GetKey (or similar) instead of Input.GetKeyDown... I have based my entire game at getting input in FixedUpdate, not unreliable at all... Input.GetKey is true at least one "frame", never changes during the FixedUpdate since it's sampled once and valid for at least one FixedUpdate, but it's true that you should not scale force by time.

 //this for example is using the gamepad in FixedUpdate with new input system
 Gamepad gamepad = Gamepad.current;
 if (gamepad != null)
 {
     Vector2 stickG1 = gamepad.rightStick.ReadValue();
     Vector2 stickG2 = gamepad.leftStick.ReadValue();
     float trgG1 = gamepad.rightTrigger.ReadValue();
     float trgG2 = gamepad.leftTrigger.ReadValue();
     //... use input here
 }

The randomness comes from using Time.deltaTime and Update() instead of Time.fixedDeltaTime with FixedUpdate(). (I need to do it in FixedUpdate where the timestep is not changing from frame to frame, since I have a replay function that is based on fixed timesteps) In my case I have set fixed time step to 100Hz (instead of the default 50Hz). It works great as long as I don't rely on unsafe functions like Input.GetKeyDown which is true only for one frame and could be missed from FixedUpdate(). Imagine doing the replay within Update and at the recording machine you have 90 fps, and at the replaying machine you have 72 fps... while FixedUpdate() runs at 100Hz timesteps for both. If you steer left for 2 timesteps during recording the replay may play for only one timestep if I use Update(), which is not good.

avatar image Eno-Khaon rh_galaxy · Oct 28, 2021 at 02:38 AM 0
Share

Well, it's worth clarifying that there's a distinct separation between Input and InputSystem. Your use of "Gamepad" and its associated variables/functions is tied to InputSystem (as you mention in the script sample), while the original question is based around Input.GetKeyDown(), an element of Input.

InputSystem has mechanisms in place to specifically and *directly* support reading input during FixedUpdate() cycles.

The older system, Input (handled via the Input Manager, mainly in the editor), does not support that ti$$anonymous$$g choice and always runs during the main Update() cycle.


Just because player input *CAN* work by avoiding the most ti$$anonymous$$g-sensitive calls doesn't mean that it's the ideal solution and can easily lead to bad habits and practices that would show up again in subsequent projects/scripts with different goals in $$anonymous$$d. Finding workarounds involving Input.GetKey() as a stand-in for Input.GetKeyDown() doesn't change the fact that you *CAN'T* rely on Input.GetKeyDown() during FixedUpdate(), and that any behavioral consistencies become framerate-dependent.

As an example of reading Input separately from its use with a Physics-driven character:
 enum ButtonState { NONE, DOWN, HELD, UP }
 ButtonState jumpInput;
 public KeyCode jumpKey = KeyCode.Space;
 void Update()
 {
     // Only looks for KeyDown and KeyUp variants,
     // then holds the result until FixedUpdate()
     if(Input.GetKeyDown(jumpKey))
     {
         jumpInput = ButtonState.DOWN;
     }
     else if(Input.GetKeyUp(jumpKey))
     {
         jumpInput = ButtonState.UP;
     }
 }
 
 void FixedUpdate()
 {
     // Uses the current state if it was set to UP or
     // DOWN during the last Update(), then cycles
     // the state to the next in sequence
     if(jumpInput == ButtonState.DOWN)
     {
         rb.AddForce(-Physics.gravity.normalized * jumpForce, ForceMode.VelocityChange);
         jumpInput = ButtonState.HELD;
     }
     else if(jumpInput == ButtonState.UP
     {
         jumpInput = ButtonState.NONE;
     }
 }

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

143 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 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

How do I convert this into Fixed Update for physics and Update for inputs? 0 Answers

Fixed Update Problems C# - Please help! 1 Answer

2D Animated tile synchronization 0 Answers

GetKeyDown Space doesn't work correctly with FixedUpdate 2 Answers

How do I pass an input in Update() to FixedUpdate() for Physics movement? 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