I have a script for a 2d space shooter game, and I want to make a limit so only 3 bullets are allowed on screen at a time
If you need it, here’s the script:
using UnityEngine;
using System.Collections;
public class PlayerLaser : MonoBehaviour
{
float speed;
public GameObject Laser;
public GameObject LaserPositionA;
// Use this for initialization
void Start ()
{
speed = 15f;
}
// Update is called once per frame
void Update()
{
//get the bullet's current position
Vector2 position = transform.position;
if (Input.GetKeyDown("space"))
{
GameObject bullet01 = (GameObject)Instantiate(Laser);
bullet01.transform.position = LaserPositionA.transform.position;
}
//compute the bullet's new position
position = new Vector2(position.x, position.y + speed * Time.deltaTime);
//update the bullet's position
transform.position = position;
//this is the top-right point of the screen
Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1))
;
if (transform.position.y > max.y)
{
Destroy(gameObject);
}
}
}
Thank you!!