# How to lerp between diferent spot angles

I have this code that Change spot angle randomly between 'minAngle' and 'maxAngle' each 'interval' seconds:

public float interval = 0.7f;
public float minAngle = 2;
public float maxAngle = 10;
float timeLeft;

Light lt;

void Start()
{
lt = GetComponent<Light>();
lt.type = LightType.Spot;

timeLeft = interval;
}

void Update()
{
timeLeft -= Time.deltaTime;

if (timeLeft < 0.0)
{
timeLeft = interval;
lt.spotAngle = Random.Range(minAngle, maxAngle);

}
}


}

but i want the angle to change gradualy, for example if the angle is 2 and the new random number is 10, i want it to look like its growing gradualy. I supose i have to use a for? or some how take in consideration the previous value and lerp it?

• There's nothing in particular about spot angles that changes the basic mathematics of how Lerp works, or how to iterate a blend over time. You can search for past Q&A about how to blend translation or rotation or colour between two values and apply virtually identical code to your case. Can you show us how you've tried to apply research like this to solve your problem, and where you're stuck? Aug 24, 2021 at 22:35
• ok im using lerp now, but i get this error: "the name lerp does not exists in the current context" this is what i wrote previousAngle = currentAngle; timeLeft -= Time.deltaTime; if (timeLeft < 0.0) { timeLeft = interval; currentAngle = Random.Range(minAngle, maxAngle); finalAngle = Lerp(previousAngle, currentAngle, timeleft / 10); lt.spotAngle = finalAngle; Aug 25, 2021 at 12:40
• i used this example: gamedev.stackexchange.com/questions/12754/… Aug 25, 2021 at 12:43
• Presumably you meant to use Mathf.Lerp, which is the name of the lerp function acting on floats in Unity? (You should also tag your question Unity C# by the way) If you search for Unity answers you'll find examples of this that you can copy more directly. You've probably also found that code does not format nicely in comments, so edit your question when you want to add information to it. Aug 25, 2021 at 12:48
• thanks gregory, will do next time. Aug 25, 2021 at 13:07

Every 'interval seconds' you set a new angle. Of course, this will just snap it instantly, and if you want it to transition smoothly, you're of course going to have to change the spot angle more times than just once every 'interval seconds'.

One solution is every time you calculate the new random angle, you get the difference between the new angle and the current angle and split up this difference into smaller bits; so instead of just taking one big step to the new angle you take smaller steps over multiple frames creating the transition effect you want.

How many steps do you break it up into though?

You would like this to transition before the next 'interval' seconds happens so you can figure out how many times the update function gets called before a new interval seconds happens. This is ceil(interval / Time.deltaTime). This is how many steps we want to break it up into. Divide the difference by this number. Now every frame the spotlight angle gets incremented by this smaller difference and once one full interval happens it will have traveled the full difference.

public float angle_increment = 0;
public float updates_per_increment = Mathf.Ceil(increment/Time.deltaTime);

void Update()
{
timeLeft -= Time.deltaTime;

if (timeLeft <= 0.0)
{
timeLeft = interval;
float new_angle = Random.Range(minAngle, maxAngle);
angle_increment = (new_angle - lt.spotAngle) / updates_per_increment;
lt.spotAngle += angle_increment;
}
else
{
lt.spotAngle += angle_increment;
}
}

• You might want to adjust your increment by Time.deltaTime to ensure it retains a smooth rate of change even if the framerate fluctuates. Aug 26, 2021 at 0:14