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!

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.