Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Vicas · Feb 13, 2013 at 03:20 PM · rotationangleeasing

Easing rotation based on angles, not speed

I am trying to make a simple speedometer / revolution counter, in which the needle eases out at certain positions after acceleration.

I can get it to accelerate nicely by just adding Time.deltaTime to itself like this:

 IEnumerator StartAcceleration(Positions toPosition)
     {
         float currentRotation = needleHub.transform.rotation.z;
         float originalRotation = needleHub.transform.rotation.z;
         float toRotation = resultRotations[toPosition].z - 0.075f
         
         float velocity = 0;    
         while (currentRotation > toRotation)
         {
             velocity += Time.deltaTime * 10f;
             // Not working
             //needleHub.transform.Rotate (0, 0, Mathf.SmoothStep(originalRotation, toRotation, velocity));
             needleHub.transform.Rotate (0, 0, velocity);
             
             currentRotation = needleHub.transform.rotation.z;
             yield return new WaitForEndOfFrame();
         }

I think this is right. It looks right anyway.

The only problem is that all examples on easing rotation I find are based on time, from 0 to 1, where smoothstep, slerps and dampangles are based on this.

But I cannot use time, since I don't know if the needle has to go from 0mph to 80mph or 0mph to 40mph. If I used time, the speed of the needle would not be the same.

So how do I smooth based on the angle in resultRotations[toPosition]?

Regards.

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 robertbu · Feb 13, 2013 at 04:02 PM

I'm a bit confused about what is driving what here. But when I do these things, I first define something that goes from 0 to 1. It might be the current velocity divided by the maximum velocity, it might be a timer with current time over total time. To get it to ease, I use an easing function.

With an easing function you map this 0 to 1 value through a curve. Sine between 0 and PI/2 works well for a lot of easing. X^Y is my second most popular curve for my easing. So if you were using a timer, velocity becomes (untested):

 velocity = Mathf.Sin(Mathf.PI / 2.0f * curTimer/totalTime) * maxVelocity;
Comment
Add comment · Show 4 · 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 Vicas · Feb 13, 2013 at 06:47 PM 0
Share

I am not sure how to map currentRotation to 0 and toRotation to 1. They represent to angles right now. $$anonymous$$g. they are respectively 0.71 and -0.73 in what I am working on right now.

$$anonymous$$y problem is that usually in the examples they make a "timer" map from 0 to 1, but I cant rely on time, since it takes different amount of time for my needle to go from 0 to 1 as it does to go from, say, 0 to 3. Does it make sense?

avatar image robertbu · Feb 13, 2013 at 08:44 PM 0
Share

For easing, you can drive your motion three ways: speed, time, or a third I'll call friction. Note that you can use time to drive a "speed" motion just by setting the time based on distance. Your number below either represent really small angles, fractions of rotations, or radians. I'm not sure which, but your travel distance will always be your end position/rotation - start position/rotation. So for your given numbers above.

 fromRotation = 0.71;
 toRotation = -.73;
 
 totalRotation = toRotation - fromRotation; // -1.44

So lets say you want your speed to be -0.4 units/per second. You can implement that in two ways. You could calculate the total time by totalRotation / -0.4 which would equal 3.6, so you would set your timer for 3.6 seconds for this rotation.

As an alternate, you could have a variable called currRotation which starts equal to the toRotation and then each frame and add -0.4 * Time.deltaTime to it each frame. The fraction is then becomes:

 fraction = (currRotation - toRotation) / totatRotation;

To get the actual rotation used you would do something like:

 actualAngle = fromRotation + totalRotation * $$anonymous$$athf.Sin($$anonymous$$athf.PI / 2.0f * fraction);

 



avatar image Vicas · Feb 14, 2013 at 08:57 AM 0
Share

Thank you for this. I think I get you.

So what I want is for the needle to go from 0 ($$anonymous$$) to 3 (max), and that takes 3 seconds. Then if I only do an animation where the needle goes from 0 go 1, it should take 1/3 of the time. Given this example i realize it would be easy just to type seconds as x/3, but I was wondering if there was a more generic formula for this kind of arithmetic.

Given the speed should not be 1/3 and there was more than 3 "steps" for instance.

avatar image robertbu · Feb 15, 2013 at 01:34 AM 0
Share

Typically when dealing with speed issues, I think about units/second. So if I have a rotation in degrees, I decide how many degrees per second I want it to move. Then in the code I multiply the degreesPerSecond variable by Time.deltaTime to calculate the movement per frame.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I fix this randomly rotating gun to an unknown degrees? 1 Answer

Camera Zoom from rotation. 0 Answers

Set rotation based on 1,1,1 style vector? How to convert vector3 to quaternion? 1 Answer

Curve player movement 1 Answer

How do i determin if an objects rotation is between 2 values on each 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