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 /
avatar image
0
Question by Link0n3 · Sep 28, 2017 at 04:33 PM · rotationquaternionfps controllerslerpsmoothing

Smooth FPS rotation with right/left arrow key

Hello community,

I am writing a simple fps controller and I am already stuck with movements. My design require rotation of the character to be performed with left/right arrows.

So my update function is:

 void Update()
 {    
             float _xMov = CrossPlatformInputManager.GetAxisRaw("Horizontal");        
             _xMov = Mathf.Abs(_xMov) > float.Epsilon ? _xMov : 0f;
     
             m_CharacterTargetRot *= Quaternion.Euler(0f, _xMov * m_RotateSpeed, 0);
     
             transform.localRotation = Quaternion.Slerp(transform.localRotation, m_CharacterTargetRot, m_SmoothRotation * Time.deltaTime);          
 }

By adjusting parameters RotateSpeed to 2 and SmoothRotation to 8 I can achieve a nice smooth rotation, but sometimes it gets "snappy" and "stiffy".

Does anyone knows how can I improve this (and maybe point out how uncorrectly am I using Slerp)?

Thank you very much,

Andrea

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

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by FortisVenaliter · Sep 28, 2017 at 07:43 PM

Yeah, I wouldn't use Slerp when you have a moving target. That's designed to smoothly move from two fixed points, so it doesn't surprise me that you end up with some unpredictable behaviour.

What you actually want is some basic calculus. Don't run away though, it is very basic. Instead of tracking only the angle, you want to track the angle and the angular velocity. When the user presses the buttons, they won't be changing their angle at all, they'll be changing their angular velocity. Then you just apply that velocity every frame to rotate the object. If the player is not pressing any rotation buttons, you apply dampening to the angular velocity to slow it back down to zero. After that, it's simply a matter of playing with the values until they stop feeling too loose or tight.

Comment
Add comment · Show 1 · 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 Link0n3 · Oct 30, 2017 at 05:37 PM 0
Share

Thanks for the hint

I actually end up using the rigid body for handling the angular velocity by adding torque to it. I am satisfied on the result, although I think there is still a better way to do that.

I found it very useful in Unity 2017 using the Force$$anonymous$$ode.Acceleration, so you can ignore the mass in the calculation and can just write your code thinking about the angularVelocity magnitude.

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

144 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 avatar image

Related Questions

Trying to get Player Character to look at the mouse pointer... which i have done, but would like to smooth the rotation. 1 Answer

Rotate on Transform.localeulerAngels smoothly 0 Answers

Can't really grasp how to keep my player level on X and Z while using LookRotation 0 Answers

How to use Quaternion.Slerp with transform.LookAt? 3 Answers

unwanted rotation in y axis 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