How to make a lightning flash (extending tutorial...)
I have followed the tutorial: https://www.youtube.com/watch?v=WRVETgdB-qw
I have copied out the code at 1m 20s and it makes the light flash on and off (in my case a direction light which is very bright).
How do i change the script to make it so when the light is on, it stays on ONLY for 2 frames of rendered animation, and then turns off... THEN follows the randomized delay?
(I am on unity 2019.2.0f1 - the topic tags appear to be like unity4/5 so i dunno what to tag this with).
Answer by crazycrinkle · Nov 24, 2020 at 03:13 PM
I should probably clarify "2 frames of rendered animation" means 2 frames/fps of gameplay, not any kind of animation, i.e. 1 30th of a second if game is running at 60fps.
I managed to solve this myself, by adding in another float variable, so now the script reads:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lightning_Flash_Fast : $$anonymous$$onoBehaviour
{
Light testLight;
public float $$anonymous$$WaitTime;
public float maxWaitTime;
public float turnOffInterval;
// Start is called before the first frame update
void Start() {
testLight = GetComponent<Light>();
StartCoroutine(Flashing());
}
IEnumerator Flashing()
{
while (true)
{
yield return new WaitForSeconds(Random.Range($$anonymous$$WaitTime,maxWaitTime));
testLight.enabled = ! testLight.enabled;
yield return new WaitForSeconds(turnOffInterval);
testLight.enabled = false;
}
}
// Update is called once per frame
void Update()
{
}
}
Your answer
Follow this Question
Related Questions
UV Mapping problem. 0 Answers
File.WriteAllBytes problem 0 Answers
In what situation Component.gameObject method throws NullReferenceException? 2 Answers
Errors in building to APK 2 Answers