Two weapons at the same time

Hi Guys, guess I am spamming this forum a bit, but also I think some questions might be interesting for more people :wink:

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 :wink: 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);
}

}

pewpew

Hi Johnny,

Good job on challenging yourself. :slight_smile:

Do you have any problem or did you just want to share your solution with us?

haha thank you :wink:

well yes - it does not work :stuck_out_tongue:

it only shoots the “bomb” but not the “pewpew”…

Does the pewpew object show up in the inspector?
For debugging, remember you can use Debug.Log or EditorApplication.isPaused.

Hi Lorgi, nope, no pew pew in the inspector

I suppose it has something to do with the update…?

Are there any error messages in your console? Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

nothing at all, no errors no nothing

if I may ask, does that code look “correct” to you, like it should do something?

Looks good to me…maybe it is instantly triggered and removed because the collision layers are not set up correctly?

I would add some debug logs to the pewpew object, to see if it is instantiated correctly and what happens to it.

2 issues:

  1. Laser : you have missed the line that decrements the interval time for the laser which is why it never fires. The missing line to be added to the function below as shown.
private void CountDownLaser()
{
    couSho -= Time.deltaTime;   // missing line
    if (couSho <= 0f)
    {
        Shootlaser();
        couSho = Random.Range(minTime, maxTime);
    }
}

  1. Laser and bombs : your code is currently set to move these horizontally instead of vertically so just switch the passed arguments around.
> Bomb
baboom.GetComponent<Rigidbody2D>().velocity = new Vector2(0 ,- velocityofbomb);

> Shootlaser
thepew.GetComponent<Rigidbody2D>().velocity = new Vector2(0 , - velocityOfPew );

Once these issues are fixed it works fine as shown :

image

Hell I really missed that line and it was the error :joy: :sob:

thanks! the horizontal thing is actually correct for me as my game will (hopefully some day) be horizontal.

Thanks Codemonk and everyone, really appreciated, until next time (so…tomorrow lol)!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.