• 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 MadJohny · Mar 01, 2014 at 09:28 AM · c#rotationpositionlerpmathf

Mathf.Lerp not working

Hi, I have been having this issue for a while now and finally decided to make a question about this since it's happenning in a lot of situations and not just only this one, the thing is, I can't get Mathf.Lerp to work, nor Vector3.Lerp, any kind of Lerp, I am trying to lerp the rotation of an object in the y axis. But I've been trying to do this in a lot of different situations, like lerping between two positions. The code just doesn't work.

this is the line of code, it is on LateUpdate:

 theRotation.y = Mathf.Lerp (transform.rotation.y, cameraTargetScript.currentYRotation, Time.deltaTime * rotateSpeed);

So, what am I doing wrong? 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

2 Replies

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

Answer by whydoidoit · Mar 01, 2014 at 09:38 AM

Well your problem there is that you are lerping a part of a quaternion which is not the y rotation you probably think it is.

    var someRotation = transform.eulerAngles;
    someRotation.y = Mathf.LerpAngle(someRotation.y, cameraTargetScript.currentYRotation, Time.deltaTime * rotateSpeed);
    transform.eulerAngles = someRotation;

Note use LerpAngle for angles as it goes the right way around (the shortest route). You have to put the rotation back into the transform and if you want actual degrees use eulerAngles not rotation.

Comment
Add comment · Show 3 · 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 whydoidoit · Mar 01, 2014 at 09:39 AM 0
Share

http://unitygems.com/quaternions-rotations-part-1-c/

avatar image MadJohny · Mar 01, 2014 at 09:57 AM 0
Share

Okay... now this is gettin weird, something seems to be wanting the rotation to snap back

avatar image MadJohny · Mar 01, 2014 at 10:06 AM 0
Share

Oh, I didn't read your script well, so it should also work, anyway just Lerp seems to be working fine

avatar image
0

Answer by SirCrazyNugget · Mar 01, 2014 at 09:54 AM

From the looks of your code Lerp isn't the function you're looking for.

Lerp interpolates between two values by a value between 0f and 1f. If you specify the two values from=60 and to=100 depending on the value given will return the interpolated value in between those two points.

so:

 Mathf.Lerp(60f, 100f, 0f); //return 60f

 Mathf.Lerp(60f, 100f, 0.25f); //return 70f

 Mathf.Lerp(60f, 100f, 0.5f); //return 80f

 Mathf.Lerp(60f, 100f, 0.75f); //return 90f

 Mathf.Lerp(60f, 100f, 1f); //return 100f

and obviously everything in between.

Comment
Add comment · Show 2 · 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 whydoidoit · Mar 01, 2014 at 09:56 AM 0
Share

Looks like he is doing a damped lerp to me - nothing wrong with that.

avatar image MadJohny · Mar 01, 2014 at 10:16 AM 0
Share

since the code runs everyframe, it should work like a damp anyway

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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

Related Questions

Flip over an object (smooth transition) 3 Answers

How do I lerp the position and rotation? 1 Answer

Is it possible to use a quaternion from a gameobjects position to another gameobjects position? 1 Answer

How to use the lerp function with rotation? (C#) 1 Answer

RTS Camera movement wrong after rotation 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges