- Home /
Question by
Faiz_Abdullah · Feb 20, 2020 at 12:32 PM ·
c#coroutineienumerator
Make a laser flickering.
Is this a good way to do it? I just set the laser on and off less than 100. I mean it is working, but it will stop once it reach 100. So yeah..
void Start()
{
StartCoroutine(LaserFlickering());
}
IEnumerator LaserFlickering()
{
while (laserOnOff < 100)
{
yield return new WaitForSeconds(2);
laser.SetActive(false);
yield return new WaitForSeconds(2);
laser.SetActive(true);
laserOnOff += 1;
}
}
}
Comment
Answer by Sxythe · Feb 20, 2020 at 02:33 PM
You could use a delay and a toggle for the activation
Something like this should do
if (m_Delay < m_MaxDelay)
{
m_Delay += Time.deltaTime;
}
else
{
m_Delay = 0;
m_Object.SetActive(!m_Object.activeSelf); //Toggles activation
}
can you help me with a little bit more? I'm trying to make player health lose whenever player collide with the laser. This is my code:
public class Flickering : $$anonymous$$onoBehaviour { public GameObject laser; public float delay; public float maxDelay;
void Update()
{
if (delay < maxDelay)
{
delay += Time.deltaTime;
}
else
{
delay = 0;
laser.SetActive(!laser.activeSelf);
}
}
public void OnTriggerEnter2D(Collider2D col)
{
HealthControl.health -= 1;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
c# Using an IEnumerator yield WaitForSeconds to temporarily pause a While loop 3 Answers
Distribute terrain in zones 3 Answers
Why Won't My Coroutine Yield? 2 Answers