- Home /
Muzzle flash stop when reloading?
Hi all, I have wrote this script that spawns a simple particle effect of a muzzle flash at the end of my gun when the LMB is clicked, I have other scripts on my gun that control shooting raycasts and reloading ect, but I'm having a problem with how I would stop the muzzle flash for being able to be spawned when the gun is reloading, I have done so with my raycasts and animations but I don't know how I would do it for this, can anyone help me out please?
var muzzleFlash : GameObject;
var spawnPoint : Transform;
function Update () {
if(Input.GetButtonDown("Fire1")){ Instantiate(muzzleFlash, spawnPoint.position, spawnPoint.rotation); } }
Answer by FLASHDENMARK · Jul 16, 2012 at 03:56 PM
As I said. You should add a reload variable to keep track of if you are reloading or not. Here is something to help you along the way:
var muzzleFlash : GameObject;
var spawnPoint : Transform;
var amountOfShots = 8;
var reloadTime = 1.5;
private var reloading = false;
function Update (){
if(Input.GetButtonDown("Fire1") && !reloading){ //Check for input and that we aren't reloading
//Exclamation mark pretty much means "is false"
Shoot();
}
}
if(Input.GetKeyDown("r")){
Reload();
}
function Reload (){
reloading = true; //Set reloading to true because... We are reloading
yield WaitForSeconds(reloadTime);
amountOfShots = 8;
reloading = false; //Set reloading to false because we are done reloading
}
function Shoot (){
//All of this below will now only execute when we are not reloading
if(amountOfShots > 0){ //If we have ammunition left
amountOfShots--;
Instantiate(muzzleFlash, spawnPoint.position, spawnPoint.rotation);
}
}
Not tested, but might work and you should be able to get an idea of what to do.
Answer by programmrzinc · Jul 14, 2012 at 09:49 PM
var muzzleFlash : GameObject;
var spawnPoint : Transform;
function Update () {
if(Input.GetButtonDown("Fire1")){
Instantiate(muzzleFlash, spawnPoint.position, spawnPoint.rotation);
}
if(Input.GetButtonUp("Fire1")){
Destroy(muzzleFlash);
}
}
this is so when the key comes up, the gameobject will destroy
my particle effect already does this, what I meant was when my gun runs out of ammo (8) it cant be spawned even if the L$$anonymous$$B has been clicked and then be able to spawn again once I have reloaded.
Just add a reloading variable. When reloading set it to true and make it so that you can only spawn your muzzle flash when reloading is false.
I have altered it to this but it doesn't work and I don't know what to do, can you help me out? sorry for the bad formatting.
var muzzleFlash : GameObject; var spawnPoint : Transform; var amountOfShots = 8; var reloadTime = 1.5;
function Update () {
if(Input.GetButtonDown("Fire1")){ Instantiate(muzzleFlash, spawnPoint.position, spawnPoint.rotation); Shoot(); } }
if(Input.Get$$anonymous$$eyDown("r")){ Reload(); }
function Reload () {
yield WaitForSeconds(reloadTime); amountOfShots = 8; }
function Shoot (){
if(amountOfShots > 0){
amountOfShots--;
} }
Answer by Blink · Jul 17, 2012 at 03:52 PM
I've figured it out now, I have posted the script below thanks for the help and directions!!
var muzzleFlash : GameObject;
var spawnPoint : Transform;
var fullClip : int = 8;
var bulletsLeft : int = 8;
function Update () {
if(Input.GetButtonDown("Fire1")){
if(bulletsLeft > 0 ){
bulletsLeft -- ;
Instantiate(muzzleFlash, spawnPoint.position, spawnPoint.rotation);
}
}
if(Input.GetButtonDown("r")){
bulletsLeft = fullClip ;
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Stop animation when no more bullets 1 Answer
Reloading Help 2 Answers
Playing animation when picking up an object. 2 Answers
Reloading wont work 1 Answer