Hey this is my gun script. Its automatic but all the bullets fly off at once. Can you tell me how to fix this?
var prefabBullet:Transform;
var shootForce:float;
var shots : int = 0;
var maxShots : int = 8;
var shootSound : AudioClip;
function Update()
{
if(Input.GetButtonDown("Fire1") && shots < maxShots)
{
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
audio.PlayOneShot(shootSound); shots++;
}
else if (shots >= maxShots && Input.GetKeyDown(KeyCode.R))
{
shots = 0;
}
}
Joshua
2
Add in a counter.
var timeInBetweenShots : float = 0.5;
private var lastShotFiredAt : float = -10.0; //start out at lower then minus the timeBetweenShots so you can fire from start.
function Update()
{
if( Input.GetButtonDown("Fire1") && shots < maxShots && Time.time > lastShotFiredAt + timeInBetweenShots)
{
lastShotFiredAt = Time.time;
//the actual shooting'
}
else if( stuff )
...
}