- Home /
How can I make a gun automatic?
Here is my gun script,
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;
}
}
How can i set this script so the gun is automatic
can you please paste the code again because it is very hard to read. When you paste it use the 101010 button. I dont really understand what you mean.
No dude thats the script. It doesn't need to be seperated out. Thats it.
Answer by Dreamblur · Jun 25, 2011 at 02:21 AM
Replace this line:
if(Input.GetButtonDown("Fire1") && shots < maxShots)
with this line:
if(Input.GetButton("Fire1") && shots < maxShots)
You'll need to put some sort of delay so you don't have a variable rate of fire based on frame rate.
e.g. if( Input... && lastShotTime + $$anonymous$$TimeBetweenShots < Time.time )
Answer by testure · Jun 25, 2011 at 02:23 AM
change GetButtonDown to GetButton.
and also try the unity docs/google: http://unity3d.com/support/documentation/ScriptReference/Input.GetButton.html
Your answer
Follow this Question
Related Questions
Bullet Collision Issues 1 Answer
Gun Sound On Mousedown 2 Answers
Need help... Start Button no longer working!!!!! 0 Answers
gun crashing 1 Answer
Play an animation every shot? 1 Answer