Boss Turrets
Create a waving fire pattern
1 min readJul 29, 2021
Using Mathf.Sin we get a nice smooth wave like fire pattern for our boss.
We rotate an empty gameobject turret that shoots lasers in the direction it’s facing. The script takes in account the rotation of the gameobject and adds directional velocity to the rigidbody of the laser.
[SerializeField] private float _fireRate = 3f;
private float _nextFire = 0f;
private float _speed = 3f;
[SerializeField] private float _rotationSpeed = 90f;
private float _rotation;
[SerializeField] private float _offset = 180f;
[SerializeField] private GameObject _enemyLaser;
void Update()
{
_rotation = _rotationSpeed * Mathf.Sin(Time.time);
_rotation += _offset;
transform.rotation = Quaternion.Euler(0, 0, _rotation);
if (Time.time > _nextFire)
{
_nextFire = Time.time + _fireRate;
GameObject bossLaser = Instantiate(_enemyLaser, transform.position, transform.rotation);
Vector3 movement = transform.rotation * new Vector3(0, _speed, 0);
bossLaser.GetComponent<Rigidbody2D>().velocity = movement;
}
}
The offset and rotation speed are different for the left and right turret.