- Home /
Disable shooting whilst reloading?
At the moment when I reload my gun I am still able to shoot. How can I change my script so that when I am reloading, shooting is disabled until it has finished? This is my script for shooting raycasts, hope someone can help me out here.
var amountOfShots = 8;
var reloadTime = 1.5;
function Update (){
if(Input.GetButtonDown("Fire1")){
Shoot();
}
if(Input.GetKeyDown("r")){
Reload();
}
}
function Reload (){
yield WaitForSeconds(reloadTime);
amountOfShots = 8;
}
var shootSound : AudioClip;
var bloodPrefab : Transform;
var sparksPrefab : Transform;
var hit : RaycastHit;
var range = 500;
var otherScript : DamageReciever;
function Shoot (){
if(amountOfShots > 0){
amountOfShots--;
if(shootSound){
audio.PlayOneShot(shootSound);
}
if(Physics.Raycast(transform.position, transform.forward, hit, range)){
var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
if(hit.collider.tag == "Enemy"){
if(bloodPrefab){
Instantiate(bloodPrefab, hit.point, rot);
}
var otherScript : DamageReciever;
otherScript = hit.collider.gameObject.GetComponent(DamageReciever);
otherScript.Damage(5);
}else{
if(sparksPrefab){
Instantiate(sparksPrefab, hit.point, rot);
}
}
}
}
}
Comment
Best Answer
Answer by Khada · Mar 17, 2013 at 04:56 PM
Basic boolean usage.
Under line 2, put a new line with:
var reloading : boolean = false;
Under line 14, put a new line with:
reloading = true;
Under line 16, put a new line with:
reloading = false;
Under line 26, put a new line with:
if(reloading)
return;
This makes me only able to shoot when I am reloading, should I just change true and false around?
change if(!reloading) to if(reloading) and it will work as stated above.
Woops, sorry about that, it's all off the top of my head so mistakes can creep in :P Cheers for answering Unitraxx