- Home /
how to fire with limited bullet ?
here is my code i just need to fire only 3 bullets but i dont understand how to do it
public Transform[] firepoint; public Transform firepoints; public Transform Firepoint; public GameObject bulletPrefab; public GameObject RocketPrefab; public GameObject GernadePrefab; public GameObject LandMine; private int currentBullets = 0; private int totalbullets = 4;
public float BulletForce = 20f;
public float GernadeForce = 20f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (GameManager.Instance.isGameInPlay == true)
{
if (Input.GetKeyDown(KeyCode.LeftControl))
{
if (CarController.instance.CurrentWeapon == 1)
{
Shoot();
}
if (CarController.instance.CurrentWeapon == 2)
{
BulletShoot();
}
if (CarController.instance.CurrentWeapon == 3)
{
Gernade();
}
if (CarController.instance.CurrentWeapon == 4)
{
Landmine();
}
}
}
}
void BulletShoot()
{
if (currentBullets > 0)
{
foreach (var firepoint in firepoint)
{
GameObject bullet = Instantiate(bulletPrefab, firepoint.position, firepoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firepoint.up * BulletForce, ForceMode2D.Impulse);
}
}
else
{
currentBullets = totalbullets;
}
}
void Shoot()
{
GameObject bullet = Instantiate(RocketPrefab, firepoints.position, firepoints.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firepoints.up * BulletForce, ForceMode2D.Impulse);
}
void Gernade()
{
GameObject bullet = Instantiate(GernadePrefab, firepoints.position, firepoints.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firepoints.up * GernadeForce, ForceMode2D.Impulse);
}
void Landmine()
{
GameObject bullet = Instantiate(LandMine, Firepoint.position, Firepoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(Firepoint.up * GernadeForce, ForceMode2D.Impulse);
}
Answer by Deuce2008 · 3 days ago
If you mean fire multiple bullets, then you should make a
public int bulletNumber
When button is pressed loop through that many times:
for(int i = 0; i < bulletNumber; i++)
{
//Your firing code here
}
actually, I want 3 bullets only fire but I didn't know how to do it in the script
Answer by Idual · 2 days ago
I'm not sure exactly what you are trying to achieve but I suspect that you want the bullets to fire sequentially.
In your bulletShoot() method you need to test if the number of bullets fired is greater than or equal to the number you want to allow.
Something like this:
if(currentBullets < 3)
{
/* code to fire your bullet */
currentBullets++;
}
You could then decrease currentBullets by one when a bullet hits it's target or exceeds it's max range.
You might also want to add a time delay between the bullets. This can be done like this.
if(currentBullets < 3 && Time.time > lastFired+1)
{
/* code to fire your bullet */
currentBullets++;
lastFired=Time.time;
}
This will give a delay of one second between bullets.
Your answer
Follow this Question
Related Questions
I cant get my 2d shooting script to work? 2 Answers
2D Shooter - shooting forward at the current rotation 2 Answers
My gun can't shoot after some adjustments to the script 1 Answer
Shooting Cannons from a boat 1 Answer
Getting 2D projectiles that fire in the direction of the mouse position to work in unity 5? 0 Answers