Hi. I have two guns on my spaceship. When the player presses space, the 2 guns are meant to fire half a second apart from each other. However, they currently fire together then wait five seconds before firing together again. How can I fix this?
thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour
{
public Transform[] Guns;
public float BulletSpeed = 10;
public float BulletSize = 20;
public GameObject BulletType;
float reloadtime = 0.5f;
float nextfiretime = 0f;
void Gunfire2()
{
if(Input.GetKey("space") && Time.time > nextfiretime)
{
foreach (Transform N in Guns)
{
GameObject Bullet = Instantiate(BulletType, N.transform.position, N.transform.rotation) as GameObject;
Bullet.transform.localScale = new Vector3(BulletSize, BulletSize, BulletSize);
Bullet.GetComponent().AddForce(Bullet.transform.forward * BulletSpeed);
nextfiretime = Time.time + reloadtime;
}
}
else
{
}
}
IEnumerator GunFire()
{
if (Input.GetKey("space"))
{
foreach (Transform N in Guns)
{
GameObject Bullet = Instantiate(BulletType, N.transform.position, N.transform.rotation) as GameObject;
Bullet.transform.localScale = new Vector3(BulletSize, BulletSize, BulletSize);
Bullet.GetComponent().AddForce(Bullet.transform.forward * BulletSpeed);
yield return new WaitForSeconds(0.5f);
}
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Gunfire2();
}
}
↧