Untitled
user_8221732482
csharp
3 years ago
3.5 kB
53
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LaserShoot : MonoBehaviour
{
public Transform firePointLaser;
// Laser GameObjects
public GameObject laser1Prefab;
public GameObject laser2Prefab;
public GameObject laser3Prefab;
// Laser Cooldown
public float laserCooldown1 = 10.0f;
public float laserCooldown2 = 15f;
public float laserCooldown3 = 20f;
// Can use a certain level of laser (1, 2, 3)
public bool canUseLaser1 = false;
public bool canUseLaser2 = false;
public bool canUseLaser3 = false;
public float laserForce = 50f; // Laser speed
void Awake()
{
StartCoroutine(LaserCooldown1st());
}
void Update()
{
if (Input.GetButton("Fire2"))
{
Laser();
}
}
void Laser()
{
if (canUseLaser3)
{
GameObject laser3 = Instantiate(laser3Prefab, firePointLaser.position, firePointLaser.rotation);
Rigidbody2D rbl3 = laser3.GetComponent<Rigidbody2D>();
rbl3.AddForce(firePointLaser.up * laserForce, ForceMode2D.Impulse);
StartCoroutine(LaserCooldown2nd());
}
else
{
if (canUseLaser2)
{
GameObject laser2 = Instantiate(laser2Prefab, firePointLaser.position, firePointLaser.rotation);
Rigidbody2D rbl2 = laser2.GetComponent<Rigidbody2D>();
rbl2.AddForce(firePointLaser.up * laserForce, ForceMode2D.Impulse);
StartCoroutine(LaserCooldown2nd());
}
else
{
if (canUseLaser1)
{
GameObject laser1 = Instantiate(laser1Prefab, firePointLaser.position, firePointLaser.rotation);
laser1.transform.parent = firePointLaser.transform;
Rigidbody2D rbl1 = laser1.GetComponent<Rigidbody2D>();
rbl1.AddForce(firePointLaser.up * laserForce, ForceMode2D.Impulse);
StartCoroutine(LaserCooldown2nd());
}
}
}
}
// First Laser Cooldown (without 3 seconds of idle)
IEnumerator LaserCooldown1st()
{
canUseLaser1 = false;
canUseLaser2 = false;
canUseLaser3 = false;
yield return new WaitForSeconds(laserCooldown1);
canUseLaser1 = true;
yield return new WaitForSeconds(laserCooldown2);
if (canUseLaser1)
canUseLaser2 = true;
else
yield break;
yield return new WaitForSeconds(laserCooldown3);
if (canUseLaser1 && canUseLaser2)
canUseLaser3 = true;
else
yield break;
}
// Cooldowns after the first cooldown (now with 3 seconds of idle)
IEnumerator LaserCooldown2nd()
{
canUseLaser1 = false;
canUseLaser2 = false;
canUseLaser3 = false;
yield return new WaitForSeconds(3f); // 3 seconds of idle after shooting the laser
yield return new WaitForSeconds(laserCooldown1);
canUseLaser1 = true;
yield return new WaitForSeconds(laserCooldown2);
if (canUseLaser1)
canUseLaser2 = true;
else
yield break;
yield return new WaitForSeconds(laserCooldown3);
if (canUseLaser1 && canUseLaser2)
canUseLaser3 = true;
else
yield break;
}
}
Editor is loading...