- Home /
Mathf.LerpAngle working once, but not again
Hi all,
I have a problem where the function on a script calling Mathf.LerpAngle works once, but not again after that. When I instantiate a particular enemy, I want it to, at random points, turn between it's current eulerAngles.y and a random one between 0 and 360. This works the first time, when I spawn the enemy, but after that it just instantly hits the random angle I picked. Anyway, here's excerpts the script:
private var turning : boolean = true;
private var minAngle : float;
private var maxAngle : float;
private var turnTimer : float;
function Update()
{
if(turning == true)
{
var currentAngle : float = Mathf.LerpAngle(minAngle, maxAngle, Time.time);
transform.eulerAngles.y = currentAngle;
if((maxAngle - transform.eulerAngles.y) < 1 && (maxAngle - transform.eulerAngles.y) > -1)
{
turning = false;
turnTimer = Time.time + Random.Range(3.0, 8);
}
}
else if(turning == false && turnTimer < Time.time)
{
minAngle = transform.eulerAngles.y;
maxAngle = Random.Range(0.0, 360);
turning = true;
}
}
Any help would be greatly appreciated!
Answer by Bunny83 · Jun 20, 2011 at 04:37 AM
What do you expect Time.time to return? It returns the seconds since you've started your game. Like every LERP (linear interpolation) the "t" value have to be between 0.0 and 1.0
That's why Time.time will work... but only during the first second. After that the value will be clamped to 1.0
I guess you want Mathf.MoveTowardsAngle.
Thanks, that worked perfectly. I thought the Time.time thing was strange too, but as it was in the API here http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$athf.LerpAngle.html I thought I just didn't know what it was supposed to do. Also, thanks for explaining what lerp actually stood for, cause that was annoying me a bit :D Thanks again!
:D yeah, some of the examples in the docs don't make much sense...
If you want to know a bit more about lerp look here:
http://en.wikipedia.org/wiki/Lerp_%28computing%29
@Bunny83 thanks for the references. I am curious about something though: anybody happens to know the differences between '$$anonymous$$athf.LerpAngle' and '$$anonymous$$athf.$$anonymous$$oveTowardsAngle', at this point? Is it just that the latter accept degrees per second rather than time?
@roamcel: The difference is that lerp is always based on a t-value (not time) this value deter$$anonymous$$es the position between start(t==0) and end(t==1).
Usually the start and end values doesn't change during lerp. Imagine two points in space and connect them with a straight line.
However some people use lerp for a decelerated movement by using the current position as start (so it's different each time you call lerp) and a small fix t-value. If you use for example 0.2 for t that would mean each frame you move 20% of the remaining distance towards the end. Since the distance gets smaller over time the 20% movement also gets smaller. That's why it's decelerated. Actually you will never reach the target that way, because you always move just 20% of the remaining distance. It can get very very small <0.00001 so visually there's no change.
$$anonymous$$oveTowardsAngle does something similar like the normal lerp but it uses a constant speed (that's the max delta value) which tells the function how much it's allowed to move each frame. It's almost the same as moving it "manually" each frame maxDelta-amount towards the target. The only difference is that is ensures to stop at the target ;) Only in the last step it will reduce the amount of movement to stop at the target.
The OP wanted a true linear movement. If you use lerp you have to calculate the t-value accordingly and move it from 0 to 1 over time.
If you want to move a value from start to end in a specific timespan, use lerp. If you want to move a value from start to end at a specific speed use $$anonymous$$athf.$$anonymous$$oveTowards or $$anonymous$$athf.$$anonymous$$oveTowardsAngle.
Your answer
Follow this Question
Related Questions
Mathf.Lerp won't stop snapping 2 Answers
Smooth Camera Angle Scroll 1 Answer
Lerp isn't completing itself 3 Answers
Lerp doesn't work after using a lerp? 1 Answer
How do I add multiple points using Lerp? 3 Answers