- Home /
gun ammo script
Here is my gun script
var prefabBullet:Transform;
var shootForce:float;
function Update()
{
if(Input.GetButtonDown("Fire1"))
{
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
}
}
How can i edit this script so it can only shoot 8 times. Then you have to press r to make it be able to shoot again?
Can $$anonymous$$acfanpro can you do this same thing with audio? Heres audi script
var shootSound:AudioClip;
function Update(){ if (Input.GetButtonDown("Fire1")) { audio.PlayOneShot(shootSound); } }
Can you edit it so that script can go off 8 times but then needs r to fix it?
Thank you very much! $$anonymous$$y First Person Controller is finaly done!
Wait a second, Error
Assets/Standard Assets/Scripts/Shooting 2.js(12,27): BCE0005: $$anonymous$$ identifier: 'shootSound'.
Answer by ckfinite · Jun 21, 2011 at 01:54 AM
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;
}
}
Answer by Heratitan · Jun 21, 2011 at 01:56 AM
To make it so you can only shoot eight times add a variable like ammoCount and then when you go to fire, check if your ammoCount is greater than zero, and if you do shoot, subtract one from the ammoCount. Then when you press R, just set ammoCount equal to eight again.
Your answer
Follow this Question
Related Questions
Gun Ammo Help 3 Answers
Setting bullet instansiate direction? help? 1 Answer
Trouble with script to fire a bullet from a handgun 1 Answer
Need Shooting C# script help 2 Answers
My gun won't fire. ( Javascript ) 1 Answer