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.