var projectile : Rigidbody;
var speed = 20;
function Update()
{
if( Input.GetButtonDown( "Fire1" ) )
{
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity =
transform.TransformDirection( Vector3( 0, 0, speed ) );
Physics.IgnoreCollision( instantiatedProjectile. collider,
transform.root.collider );
}
}
Add a boolean “IsAuto”
if(IsAuto)
//Check ammo. If 1 set IsAuto = false. If 0 than do an immediate return. Otherwhys continue
Adding things like burst will require a bit more work - you might want to look at this thread to have a rounds-per-minute style variable.
http://forum.unity3d.com/threads/61453-Run-a-task-10-times-per-second
I would do it this way.
var projectile : Rigidbody;
var speed = 20;
var isAuto : bool;
var roundsPerMinute = 300.0;
var lastShotTime = 0.0;
var timeBetweenShots = 0.0;
function Start()
{
timeBetweenShots = 60.0 / roundsPerMinute;
}
function Update()
{
if( isAuto ? (Input.GetButton("Fire1") Time.time - lastShotTime > timeBetweenShots) : Input.GetButtonDown( "Fire1" ) )
{
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation );
instantiatedProjectile.velocity =
transform.TransformDirection( Vector3( 0, 0, speed ) );
Physics.IgnoreCollision( instantiatedProjectile. collider,
transform.root.collider );
lastShotTime = Time.time;
}
}
Like Windex said, you’ll still want a check for making sure you have enough ammo. And this script is totally untested, but I think it will work. You’ll want to make sure you set isAuto to true if you want the gun to be automatic.
Simple, but works.
private var loaded = true;
var rps : float = 30;
function FixedUpdate () {
if (Input.GetMouseButton(0) loaded){
load();
//Fire bullet
}
}
function load()
{
loaded = false;
yield WaitForSeconds (1/rps);
loaded = true;
}
Only problem with this is that the max rps (rounds per seconds) is limited by the update function speed. So you will have to adapt code for really fast firing weapons.