- Home /
Question by
jimmyismike · Aug 21, 2011 at 09:25 PM ·
shootreload
Shoot script help
Hey can you help me. I have a shoot script that lets you reload. Can you tell me how to set it so you can't reload if you have full ammo.
My script
var prefabBullet:Transform;
var shootForce:float;
var shots : int = 0;
var maxShots : int = 0;
var shootSound : AudioClip;
function Update()
{
if(Input.GetMouseButtonUp(0) && 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;
}
}
Comment
Best Answer
Answer by YikYikHeiHei · Aug 22, 2011 at 12:20 AM
In else if (shots
You have to change the shots to shots != maxShots
And not to use else
And the shots have to shots = maxShots
Here the full script
var prefabBullet:Transform;
var shootForce:float;
var shots : int = 0;
var maxShots : int = 30;
var shootSound : AudioClip;
var reloadTime : float = 1.0; //<--if you want to wait a time to reload
var reloading : boolean = false; //<--if you want to wait a time to reload
function Update()
{
if(!reloading && Input.GetMouseButtonUp(0) && shots < maxShots)
{
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
audio.PlayOneShot(shootSound);
shots--;
}
if (!reloading && shots != maxShots && Input.GetKeyDown(KeyCode.R))
{
Reload();
}
}
function Reload()
{
reloading = true;
yield WaitForSeconds (reloadTime);
shots = maxShots;
reloading = false;
}
Thanks can you also tell me how to add a sound the reloading
Change to this
var prefabBullet:Transform;
var shootForce:float;
var shots : int = 0;
var maxShots : int = 30;
var shootSound : AudioClip;
var reloadTime : float = 1.0; //<--if you want to wait a time to reload
var reloading : boolean = false; //<--if you want to wait a time to reload
var reloadSound : AudioClip; //<--if you want to have a reload sound
function Update()
{
if(!reloading && Input.Get$$anonymous$$ouseButtonUp(0) && shots < maxShots)
{
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
audio.PlayOneShot(shootSound);
shots--;
}
if (!reloading && shots != maxShots && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R))
{
Reload();
}
}
function Reload()
{
audio.PlayOneShot(reloadSound); //<-- make reload sound!
reloading = true;
yield WaitForSeconds (reloadTime);
shots = maxShots;
reloading = false;
}
Your answer
Follow this Question
Related Questions
Wrote a script for Shooting, reloading and to display the AMMO in a GUIText 1 Answer
No Reload Script Works 1 Answer
reload help 0 Answers
Shoot Script Reload Help 1 Answer