Giant Rotating Laser Part 2

Improving the Boss’ special attack

Tristan Engel
2 min readJul 28, 2021

In this article we make a few changes to be able to reuse the Giant Rotating laser attack of our enemy boss.

Improvements made

1 Created as a projectile prefab
2 VFX when the laser is turned on with delay
3 Play SFX when turning on the laser
4 VFX When laser in on start SFX loop
5 Make sure to turn AudioSource Loop: On
6 Self destruct after X seconds
7 Boss attack script to instantiate the giant lasers
8 Giant laser instantiated as a child so it move with the boss

Giant Rotating Laser Projectile Script

By disabling the sprite renderer we created a turn on VFX. We put a play in awake laser turning on SFX on the audiosource component of the giant laser game object. This way when the giant laser is instantiated it will start turning on first with SFX then enable the sprite renderer. When the sprite renderer is enabled we set the audiosource to loop and change the clip to a continuous laser beam SFX.

//giant laser config
[SerializeField] private float _speed = 15f;
[SerializeField] private float _TurnOnGiantLaserDelay = 1.5f;
[SerializeField] private float _damageInterval = 0.5f;
[SerializeField] private float _nextDamage = 1.5f;
[SerializeField] private float _minDuration = 10f;
[SerializeField] private float _maxDuration = 20f;

//handle
private SpriteRenderer _spriteRenderer;
private AudioSource _audioSource;

[SerializeField] private AudioClip _giantLaserSFX;

private void Start()
{
_audioSource = GetComponent<AudioSource>();
_spriteRenderer = GetComponent<SpriteRenderer>();
_spriteRenderer.enabled = false;
StartCoroutine(TurnOnGiantLaser());
_nextDamage = _TurnOnGiantLaserDelay + Time.time;

//random rotate direction
if (Random.value > 0.5f)
{
_speed *= -1;
}
Destroy(gameObject, Random.Range(_minDuration, _maxDuration));
}

IEnumerator TurnOnGiantLaser()
{
yield return new WaitForSeconds(_TurnOnGiantLaserDelay);
_spriteRenderer.enabled = true;
_audioSource.clip = _giantLaserSFX;
_audioSource.loop = true;
_audioSource.Play();
}

void Update()
{
if (_spriteRenderer.enabled == true)
{
transform.Rotate(Vector3.forward * (_speed * Time.deltaTime));
}
}

private void OnTriggerStay2D(Collider2D other)
{
if (other.tag == "Player" && _nextDamage < Time.time)
{
_nextDamage = Time.time + _damageInterval;

Player player = other.GetComponent<Player>();
player.Damage();
}
}

Boss Giant Rotating Laser Attack Script

Since it’s a projectile now we can use an boss attack script the instantiate the giant laser with it.
We also instantiate the projectile as a child so it moves along with the boss.

[SerializeField] private float _minInterval = 10f;
[SerializeField] private float _maxInterval = 30f;
[SerializeField] private GameObject _giantLaser;

void Start()
{
StartCoroutine(FireRoutine());
}

IEnumerator FireRoutine()
{
yield return new WaitForSeconds(Random.Range(_minInterval, _maxInterval));
GameObject giantLaser = Instantiate(_giantLaser, transform.position, Quaternion.identity);
giantLaser.transform.SetParent(this.transform);
}

--

--

Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.