Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Xarbrough · Jul 15, 2015 at 09:57 PM · c#inputframerategetaxisaddtorque

Achieve framerate independent AddTorque rotaion with mouse drag.

 private float _rotationInput;
 
 void Update()
 {
     // Cache delta movement while dragging.
     if(Input.GetMouseButton(0))
         _rotationInput = Input.GetAxis("Mouse X");
     else
         _rotationInput = 0f;
 }
 
 void FixedUpdate()
 {
     _rigidbody.AddTorque(_rotationInput, ForceMode2D.Force);
 }

I am adding physics rotation to a Rigidbody2D by dragging with the mouse. This works the way I want it to, but only for a specific framerate.

My problem: The higher my framerate grows, the slower my rigidbody rotation torque acceleration becomes. Although it is stated, that GetAxis for mouse movement is framerate independent, it gives totally different values.

I'm looking at the Debug.Log statement of my _rotationInput and at a framerate of 30 it gives me values between -5 to 5 when slowly dragging, but at a framerate of 120 it shrinks down to -0.5 to 0.5.

I'm testing in the editor by disabling VSync and setting the targetFrameRate, which seems to work fine, since I'm measuring fps with the tool from Standard Assets, which usually is pretty accurate.

Since the GetAxis is most likely not to change soon, is there a way I can circumvent my problem? Of course, I've tried multiplying or dividing by Time.deltaTime in Update or applying the different deltaTimes in FixedUpdate etc, but the effect was always the same, just reversing whether higher framerate becomes a higher or lower delta movement.

Thanks for any ideas!

PS: I know that I wont get a fixed speed by using AddTorque and I will limit the max speed and such things, but the input directly affects the acceleration of the physics rotation, which feels quite different for the player.

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

Answer by maccabbe · Jul 16, 2015 at 02:46 AM

First, Input.GetAxis values are meant to be used in Update since they correct for the frame rate. You need to be careful using them FixedUpdate as this correction is the opposite of what you need. In this case, it seems like you are using mouse delta (change in cursor position) which will be smaller the faster it is updated. I can think of three possible solutions.

1 Apply the torque in the Update function.

 void Update(){
     if(Input.GetMouseButton(0)){
          _rigidbody.AddTorque(_rotationInput, ForceMode2D.Force);
     }
 }

2 Cache the input since the last Fixed Update instead of the last Update

 private float _rotationInput;
  
 void Update()
 {
      // Cache delta movement while dragging.
     if(Input.GetMouseButton(0))
         _rotationInput += Input.GetAxis("Mouse X");
     else
         _rotationInput = 0f;
 }
  
 void FixedUpdate()
 {
     _rigidbody.AddTorque(_rotationInput, ForceMode2D.Force);
     _rotationInput = 0f;
 }

3 Use unprocessed inputs using Input.mousePosition instead of Input.GetAxis

 private bool mouseDown=false;
 private float oldPosition;
      
 void Update()
 {
     if(Input.GetMouseButton(0)){
          if(!mouseDown){
              oldPosition=Input.mousePosition.x;
              mouseDown=true;
          }         
     }
     else{
         mouseDown=false;
     }
 }
  
 void FixedUpdate()
 {
     _rigidbody.AddTorque(Input.mousePosition.x-oldPosition, ForceMode2D.Force);
     oldPosition=Input.mousePosition.x;
 }
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 Owen-Reynolds · Jul 16, 2015 at 02:38 AM

You can reproduce GetAxis by simply saving the previous position and subtracting. But I assume this is what GetAxis is doing. The trick is automatically sort-of frame-rate independent. If you drag your finger X amount, the sum over all frames adds to X.

I'm also not sure this is the right way to add spin to an object. Seems like the rate of change each frame (so, divided by deltaTime to normalize) should be compared to the current spin speed. Otherwise a slow drag will send it spinning as much as a fast one.

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

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

23 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

Related Questions

Why does holding down any button cause ~20-30 FPS loss? 1 Answer

Multiple Cars not working 1 Answer

How can I prevent Input.GetAxis from sporadically reporting deltas of 0.0? 0 Answers

Distribute terrain in zones 3 Answers

Do i need to use Time.Deltatime while using mouse axes for rotation? 1 Answer


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