0

I have a little question. I would like to increase the Light.spotAngle property over period of time. The written code works but I want this increase with speed, or something like that. I want to increase the value at spot angle at some speed, not to be directly 100, but from 30 slowly to grow by 10 to 100.

Transform thisLight = lightOB.transform.GetChild(0);
Light spotA = thisLight.GetComponent<Light>();
spotA.spotAngle = 100f;

I tried with Time.DeltaTime, but is not work. Help!!!

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Your question is not complete. Increase it from where to where? When should you start to increase and when should you stop? – Programmer Feb 01 '18 at 11:55
  • well, now at my spot angle it's at 30, and I need to get up to 100 – Anghelina Guznac Feb 01 '18 at 11:56
  • Then change it to 100..What's the issue? – Programmer Feb 01 '18 at 12:02
  • when I click on an object, this index needs to grow slowly to 100 – Anghelina Guznac Feb 01 '18 at 12:06
  • you just did not understand what I'm thinking about – Anghelina Guznac Feb 01 '18 at 12:07
  • I can change the index directly, as in the above code, but this increase is not visible – Anghelina Guznac Feb 01 '18 at 12:08
  • When you say, index, you man the `spotAngle` property? Please edit your question to clarify your issue. – Programmer Feb 01 '18 at 12:13
  • yes, the index is the value at the spot angle. What's not clear here. I want to increase the value at spot angle at some speed, not to be directly 100, but from 30 slowly increase by 10 to 100.I hope you now understand that I do not know how to explain it anymore – Anghelina Guznac Feb 01 '18 at 12:17
  • 1
    I understand from your last 2 comment. Just edit it to your question so that people who want to help you can read that instead of scrolling through the comment to find the actually question. Also add the code with the ` Time.DeltaTime` that didn't work for you. Maybe someone can figure out why it didn't work. – Programmer Feb 01 '18 at 12:20

1 Answers1

1

Use Mathf.Lerp to lerp from value a to b. The a value is 10 and the b value is 100 according to your question. Do that in a coroutine function. that gives you control over how long you want that slow tansistion to happen. When you click an Object, start the coroutine function.

The coroutine function:

IEnumerator increaseSpotAngle(Light lightToFade, float a, float b, float duration)
{
    float counter = 0f;

    while (counter < duration)
    {
        counter += Time.deltaTime;

        lightToFade.spotAngle = Mathf.Lerp(a, b, counter / duration);

        yield return null;
    }
}

Will change Light's SpotAngle from 10 to 100 within 5 seconds.

public Light targetLight;

void Start()
{
    StartCoroutine(increaseSpotAngle(targetLight, 30, 100, 5));
}

See this post for how to detect click on any GameObject.

Programmer
  • 121,791
  • 22
  • 236
  • 328