Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 elonybear · Mar 04 at 06:11 AM · rotationmovementlerp

Mathf.Lerp working in one direction but not the other

Hi folks, I'm making a flying game with airplanes and am looking to create a visual effect when the player is flying upwards or downwards (vertical axis). Simply, I'm currently trying to rotate the plane body about 30 degrees off the start rotation around the x axis. The player body is a child of an empty game object.

alt text

I'm using Lerp to accomplish this, but it doesn't seem to be working as expected. It works perfectly well when flying upwards but not the case when flying downwards. Here is my code:

 void RotateBody()
   {
     float currentRotation = playerBody.localRotation.eulerAngles.x;
     float targetRotation = bodyTurnRotation * verticalMovement + startRotation;
     float lerpedValue = Mathf.Lerp(currentRotation, targetRotation, bodyTurnRotationSpeed * Time.deltaTime);
 
     playerBody.localRotation = Quaternion.Euler(new Vector3(lerpedValue, 0, 0));
   }

I log out all the values to the console and the Lerp value is correctly changing but the localRotation doesn't actually change! This only happens when flying downwards, it works as expected when flying upwards. Any help would be greatly appreciated!

unity-screenshot.png (7.6 kB)
Comment
Add comment · Show 2
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 jackmw94 · Mar 04 at 09:43 AM 0
Share

Does bodyTurnRotationSpeed ever change? If not I'm surprised that this works moving upwards! If it does change then could you give more information about where that's set.

Aside from this issue, it's not great practice to move between rotations using Mathf.Lerp: say you wanted to lerp between 5 degrees and 355 degrees, since Mathf.Lerp doesn't care about degrees it would take the long route and hit 180 degrees at half way instead of 0. It's possible that this could be at play here. It's preferable to use Quaternion.Lerp for lerping between rotations.

avatar image elonybear · Mar 04 at 03:10 PM 0
Share

bodyTurnRotationSpeed does not change. That's a good point about the degrees, as it so happens the degrees for the body turn stay clamped to between 60 and 120 degrees (90 is the default position).

I can and definitely will use Quaternion, but I guess I wanted to confirm my understanding of Lerp, since you mentioned that you're surprised it worked at all if bodyTurnRotationSpeed doesn't change. Lerp(a, b, t) will produce the next value from a to b with step t. So t (whch is determined by bodyTurnRotationSpeed) is just a step value, right? Why should this need to change? The only one that should change (per my understanding) is a (currentRotation in this case)?

1 Reply

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

Answer by highpockets · Mar 04 at 11:22 PM

Your understanding is wrong about Lerp. The timescale value (3rd parameter) is the distance between the 2 values and it is the only value that should change if you want to do a pure linear interpolation. Well you could calculate it without Lerp, but that’s irrelevant here.

—————-

If you want to go from 10 - 50. Inside the Lerp function, 10 will be a time scale value of 0 and 50 will be a time scale value of 1. So, with the code you are using above, you are not actually doing a linear interpolation at all. A linear interpolation would keep the same start and end positions throughout the duration of the Lerp and just change the timescale evenly on every update. Instead you are getting more of a dampened change from A to B, because you keep moving “A” closer to “B” and so the distance is shorter, but your time scale is more or less the same for every update.

——————

So, let’s say the initial distance is 40 with A at 10 and B at 50. If your timescale is 0.5, it will return 30. So now 30-50 and say your timescale is again 0.5. It will return 40. So as you see, the closer you get, the more you slow down and you may never actually reach the destination because even when you get to a point where A is at 49.9999 and B at 50, 0.5 is still halfway between the 2 values.

——————

So now to go in the opposite direction, it is the exact same, but A is now 50 and B is now 10 and a timescale of 0 is 50 and a timescale of 1 is 10.

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 elonybear · Mar 07 at 03:25 AM 0
Share

This makes sense, thank you!

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

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

rotate object by list values 0 Answers

Top down Shooter upper body look at mouse pos 1 Answer

Player model rotates forward with camera 0 Answers

click to move workig script!! but pls help with rotation!! :( 1 Answer

Moving forward based on 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