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 /
This question was closed Aug 20, 2016 at 06:20 PM by The_tubby1 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by The_tubby1 · Apr 03, 2016 at 03:17 PM · c#rotationmovementrigidbodyplayer movement

Smooth Rotation on WASD keys pressed?

Hello there,

I have been searching for the longest time for a way for my player to rotate smoothly on each key pressed. I've tried everything available to attempt to get it to work, so, here I am.

I am 80% sure that I haven't got the most efficient movement script, but it works. I'm more than willing to improve it later but for now, I would just like to know how I can get my player to rotate smoothly.

when I press "W" my player moves in the desired direction, the same goes for "A", "S", and "D". I do this using rigidbody.velocity. like this - rb.velocity = new vector3 (0, 0, 4); (rb = Rigidbody). there is definitely a better way of moving probably using time.deltatime or something.

I also have this working at an angle so when "W" and "D" are pressed for example, it moves 45 degrees in the right direction.

My Rotation code is the problem as far as i'm concerned, I'm hoping you can tell me if there is something i can add for smooth rotation from "D" to "W" for example.

My rotation code is this - transform.localRotation = quaternion.Euler(-90, -90, 0); this rotates my player (a rectangular box) to face the key pressed. However, the rotation is snappy. Is there a way using Lerp or Slerp or something else that can rotate my player to face the right direction in a smooooth way.

Sorry about the long winded question, I just want to be extremely thorough for your better understanding.

I haven't got much experience with code since I'm more about the art stuff so go easy ;)

(C# code if any please.)

Thanks in advance

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

  • Sort: 
avatar image
1
Best Answer

Answer by Lewwwer · Jul 16, 2016 at 09:35 PM

Hello!

To make things smoother try to set the variables time based. So given that you want to get something from a to b you can use a variable starting with a and add delta time and clamp it to make it the desired. Also moving a variable from 0 to 1 and using lerp you can make even more things smoother.

 float t = 0f;
 float animSpeed = 1f;
 bool animate = false;
     
 void Update()
     {
     if(animate)
     {
     t += animSpeed * Time.deltaTime*speed;
     t = Mathf.Clamp01(t);
     transform.rotation = Quaternion.Slerp(from, to, t);
     }else
     {
     t = 0f;
     }
 }

you can set the from to the current rotation you have and set the to to the desired rotation, in your example from = Quaternion.identity; to = Quaternion.Euler(-90, -90, 0);

Also this just interpolates between the from and to. When I want to make things even smoother I add a function

 float smooth(float t)
 {
 return 3*t*t-2*t*t*t;
 }


And instead Slerp(from, to, t) you can use Slerp(from, to smooth(t)); and it will make animations even smoother.

Hope it helps!

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 The_tubby1 · Jul 19, 2016 at 03:23 PM 0
Share

I will have to give this a try when i get the chance. Thanks for the detailed answer. :D I'll accept this as it seems correct ;).

Follow this Question

Answers Answers and Comments

140 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

Related Questions

moving an object continuously between waypoints 0 Answers

Move Homing Mislle (3D) 0 Answers

Issue with wave-like motion when rotating with mouse input 0 Answers

How to access y rotation of an object as a variable or value 1 Answer

Issues Getting my GameObject to both Move Forward and Jump properly. 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