[C#] A pulsating laser beam.

Hello. I want to have a laser that pulsates open / closed. I want it to be open for 3 sec then closed for 3 sec and to repeat that over and over again.

This is my code.

using UnityEngine;
using System.Collections;

public class Laser : MonoBehaviour {

    public Player P;

    public GameManager GM;

    public enum Type {
        Pulsating,
        Closed
    };

    public Type LaserType;

    public float Timer;

    void Update() {
        if (LaserType == Type.Pulsating) {
            Timer =+ Time.time;
            if (Timer > 3f) {
                this.transform.FindChild("Laser").GetComponent<SpriteRenderer>().enabled = false;
                this.GetComponent<BoxCollider2D>().enabled = false;
            } if (Timer > 6f) {
                this.transform.FindChild("Laser").GetComponent<SpriteRenderer>().enabled = true;
                this.GetComponent<BoxCollider2D>().enabled = true;
            } if (Timer > 9f) {
                Timer = 0f;
            }
        } else if (LaserType == Type.Closed) {
            if (Input.GetKeyDown (KeyCode.E) && GameObject.Find("EffectZone").GetComponent<EffectZone>().PlayerInZone == true && GM.Keys > 0) {
                this.transform.FindChild("Laser").GetComponent<SpriteRenderer>().enabled = false;
                this.GetComponent<BoxCollider2D>().enabled = false;
                GM.Keys--;
            }
        }
    }

    void OnTriggerEnter2D(Collider2D other) {
        if (other.name == "P1") {
            P.Kill(1);
        }
        if (other.name == "P2") {
            P.Kill(2);
        }
        if (other.name == "P3") {
            P.Kill(3);
        }
    }
}

It does work but not how I want it to. It counts and when it gets to 3 it disables the laser beam, when it gets to 6 it enables it and when it gets to 9 I want the float Timer to be 0 and continue on counting but it becomes zero and doesn’t count. Please help me.

Use Mathf.Repeat to wrap a value between 0 and length. When value crosses length, it starts over from 0 again.
So for a span [0, length] we can define the first half as being on and the other half as being off.

    static bool OnOffCycle(float value, float length)
    {
        return Mathf.Repeat(value, length) < (length / 2);
    }

And you could use it like this:

    bool pulseOnOff = OnOffCycle(Time.time, 6); // 3 secs on, 3 secs off = 6 sec length cycle

And the full script is here

using UnityEngine;

public class Laser : MonoBehaviour
{
    public Player P;
    public GameManager GM;

    public enum Type
    {
        Pulsating,
        Closed
    };

    public Type LaserType;

    void Update()
    {
        if (LaserType == Type.Pulsating)
        {
            bool pulseOnOff = OnOffCycle(Time.time, 6);
            EnableLaser(pulseOnOff);
        }
        else if (LaserType == Type.Closed)
        {
            if (Input.GetKeyDown(KeyCode.E) && GameObject.Find("EffectZone").GetComponent<EffectZone>().PlayerInZone == true && GM.Keys > 0)
            {
                EnableLaser(false);
                GM.Keys--;
            }
        }
    }

    static bool OnOffCycle(float value, float length)
    {
        return Mathf.Repeat(value, length) < (length / 2);
    }

    void EnableLaser(bool enabled)
    {
        transform.FindChild("Laser").GetComponent<SpriteRenderer>().enabled = enabled;
        GetComponent<BoxCollider2D>().enabled = enabled;
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.name == "P1")
            P.Kill(1);
        if (other.name == "P2")
            P.Kill(2);
        if (other.name == "P3")
            P.Kill(3);
    }
}