- Home /
How to Lerp for a number to get BIGGER instead of smaller?
I'm trying to write code that lerps the camera's rotation from where it currently is, to the player's facing angle. If the angle is less than 180, the lerp works fine. However if it's GREATER than 180, the code will still lerp the angle, but since the lerp value is getting smaller and smaller as it approaches 0 (the relative y rotation 0 means the camera is facing the same way as the player), it circles all the way around the player rather than just making the semi-circle
So right now, to get to 0, it's going
150...140...130...........20..10..0 AND 270...260...250........20..10..0
But I want it to go
150...140...130...........20..10..0, and 270...280...290........340..350..360 (360 == 0)
Here is my code:
void Update()
{
if(isLerping)
{
float yVal;
if(transform.eulerAngles.y < 180f)
yVal = Mathf.Lerp(transform.rotation.eulerAngles.y, transform.parent.transform.rotation.eulerAngles.y, lerpSmooth * Time.deltaTime);
else
//(HOW???)
transform.eulerAngles = new Vector3(transform.eulerAngles.x, yVal, transform.eulerAngles.z);
if(transform.rotation.eulerAngles.y == transform.parent.transform.rotation.eulerAngles.y)
isLerping = false;
}
}
not adding it as an 'answer' because it's just a guess, but might you not want to be using $$anonymous$$athf.LerpAngle ? http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.LerpAngle.html
code example even seems to cover this use case...
This did it perfectly :) Didn't know this existed! Thanks :)
Answer by Anymeese · Mar 16, 2014 at 08:06 AM
tobyfee's answer was correct (use Mathf.LerpAngle)
Your answer
Follow this Question
Related Questions
Making an object bounce, using Mathf.Lerp 1 Answer
Trying to lerp in different states in a switch statement 0 Answers
How the heck does Mathf.Lerp work? 2 Answers
how to use time on lerp 2 Answers
Mathf.Lerp not working 2 Answers