Hi Guys, guess I am spamming this forum a bit, but also I think some questions might be interesting for more people
I am trying to give one enemy two different weapons at the same time, so he bombs and laserz like a boss should do. Anyway I know there are probably a ton of easier ways to do it, but for good learning, I wrote a new script and basically try to call 2 things in the update method: a bomb and a laser.
NOW I am aware my naming is…interesting, but I had no other ideas what happens is that the enemy is indeed bombing, but not lasering as it should…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bomber : MonoBehaviour
{
[Header(“Bomber Stats”)]
[SerializeField] float health = 100f;
[SerializeField] int scoreValue = 150;
[Header("Bombing")]
[SerializeField] float bomberhealth = 100;
[SerializeField] float counterShot;
[SerializeField] float minTimeBetween = 0.2f;
[SerializeField] float maxTimeBeween = 3f;
[SerializeField] GameObject boom;
[SerializeField] float velocityofbomb = 10f;
[Header("Lasering")]
[SerializeField] float couSho;
[SerializeField] float minTime = 0.3f;
[SerializeField] float maxTime;
[SerializeField] GameObject pewpew;
[SerializeField] float velocityOfPew = 10f;
[Header("The Sound")]
[SerializeField] GameObject deathVFX;
[SerializeField] float durationOfExplosion = 1f;
[SerializeField] AudioClip deathSound;
[SerializeField] [Range(0, 1)] float deathSoundVolume = 0.7f;
[SerializeField] AudioClip shootSound;
[SerializeField] [Range(0, 1)] float shootSoundVolume = 0.25f;
// Start is called before the first frame update
void Start()
{
counterShot = Random.Range(minTimeBetween, maxTimeBeween);
couSho = Random.Range(minTime, maxTime);
}
// Update is called once per frame
void Update()
{
CountDownShoot();
CountDownLaser();
}
private void CountDownShoot()
{
counterShot -= Time.deltaTime;
if (counterShot <= 0f)
{
Bomb();
counterShot = Random.Range(minTimeBetween, maxTimeBeween);
}
}
private void Bomb()
{
GameObject baboom = Instantiate(
boom,
transform.position,
Quaternion.identity
) as GameObject;
baboom.GetComponent<Rigidbody2D>().velocity = new Vector2(-velocityofbomb, 0);
}
private void CountDownLaser()
{
if (couSho <= 0f)
{
Shootlaser();
couSho = Random.Range(minTime, maxTime);
}
}
private void Shootlaser()
{
GameObject thepew = Instantiate(
pewpew,
transform.position,
Quaternion.identity
) as GameObject;
thepew.GetComponent<Rigidbody2D>().velocity = new Vector2(-velocityOfPew, 0);
}
private void OnTriggerEnter2D(Collider2D other)
{
DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
if (!damageDealer) { return; }
ProcessHit(damageDealer);
}
private void ProcessHit(DamageDealer damageDealer)
{
health -= damageDealer.GetDamage();
damageDealer.Hit();
if (health <= 0)
{
Die();
}
}
private void Die()
{
FindObjectOfType<GameSession>().AddToScore(scoreValue);
Destroy(gameObject);
GameObject explosion = Instantiate(deathVFX, transform.position, transform.rotation);
Destroy(explosion, durationOfExplosion);
AudioSource.PlayClipAtPoint(deathSound, Camera.main.transform.position, deathSoundVolume);
}
}