0
\$\begingroup\$

So I'm making a bullet hell game and I have script for a basic bullet to spawn and shoot towards the player, but it's always at set intervals and I want it to be random, but I dont know how. I tried using stuff I found online, but now it sometimes shoot after 45 seconds and also every game object with the script shoot at same time. Is it possible to make it so they can shoot separately or do I have to make specific script for all of them?

Summon bullet code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemyPewPewer : MonoBehaviour
{
    public GameObject bullet;
    public Transform bulletPos;
    private float timer;
    private bool gameStarted = false;
    private float m_FireTime = 1f;
    public Vector2 m_Offset;

    void Update()
    {
        if(gameStarted)
        {
            timer += Time.deltaTime;

            if(timer > m_FireTime)
            {
                timer = 0f;
                this.m_FireTime = Time.time + (float)Random.Range(this.m_Offset.x, this.m_Offset.y);
                shoot();
            }
        }
        
        if (Input.GetKeyDown(KeyCode.Space))
        {
                GameStart();
        }
    }

    void GameStart()
    {
        gameStarted = true;
    }

    void shoot()
    {
        Instantiate(bullet, bulletPos.position, Quaternion.identity);
    }
}

Bullet code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemyPewPew : MonoBehaviour
{
    private GameObject player;
    private Rigidbody2D rb;
    public float force;
    private float bulletTimer;
    private bool gameEnded = false;
    void Start()
    {
       rb = GetComponent<Rigidbody2D>();
       player = GameObject.FindGameObjectWithTag("Player");

       Vector3 direction = player.transform.position - transform.position;
       rb.velocity = new Vector2(direction.x, direction.y).normalized * force;

       float rot = Mathf.Atan2(-direction.y, -direction.x) * Mathf.Rad2Deg;
       transform.rotation = Quaternion.Euler(0,0,rot+90);
    }
  
    void Update()
    {
        if(gameEnded)
        {
           bulletTimer = 7.5f;
           if(bulletTimer>7.5)
           {
                Destroy(gameObject);
           }
        }
        bulletTimer += Time.deltaTime;
        if(bulletTimer>7.5)
        {
            Destroy(gameObject);
        }
    }

   void OnTriggerEnter2D(Collider2D other)
   {
        if(other.gameObject.CompareTag("Player"))
        {
            GameEnd();
            Destroy(gameObject);
        }
   } 

   void GameEnd()
   {
        gameEnded = true;
   }
}
```
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You have a few logic problems with your script. I'm going to address the ones in your enemyPewPewer with comments. You need to create two variables for the range you want to have your shooting done.

void Update() {
    if (gameStarted) {
        timer += Time.deltaTime;    
        if(timer > m_FireTime) {
            //this is more accurate if you have short intervalls to not skip 
            timer -= this.m_FireTime;
            //we want a range once we fire the next bullet.                
            this.m_FireTime = (float)Random.Range(this.minFireDelay, this.maxFireDelay);
            shoot();
        }
    }
    // currently does not prevent anything, but maybe your GameStart code will have more logic like reset points, location or bullet counts.       
    if (Input.GetKeyDown(KeyCode.Space) && !gameStarted) {
            GameStart();
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Ok ill try it out \$\endgroup\$
    – Matt Sou
    Commented Jul 1 at 18:05
  • \$\begingroup\$ Thnx it worked and I modified it a bit so it's also random in it's first instance by using void start \$\endgroup\$
    – Matt Sou
    Commented Jul 1 at 18:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .