x


Stop animation when no more bullets

I have a script on my gun which triggers animations on button press and another script which fires rays and reloads my gun ect. But how would I alter this so that if there are no bullets left I cannot play the shoot animation until there are, can anyone help me?

animations script

function Start () {

animation.wrapMode = WrapMode.Loop;

animation["idle"].speed = 0.2;

animation["walk"].speed = 1;

animation["reload"].speed = 2.5;

animation["shoot"].speed = 2;

animation["shoot"].wrapMode = WrapMode.Once;

animation["reload"].wrapMode = WrapMode.Once;

animation["shoot"].layer = 1;

animation["reload"].layer = 2;

animation.Stop();

}

function Update (){

if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)

animation.CrossFade("walk");

else

animation.CrossFade("idle");

if(Input.GetButtonDown("Fire1")){

animation.Stop();

animation.Play("shoot");

}

if(Input.GetButtonDown("r")){

animation.Stop();

animation.CrossFade("reload");

}

}

raycast, reload script

var amountOfShots = 8;

var reloadTime = 1;

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;

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);

    }

        hit.collider.gameObject.SendMessage("ApplyDamage", 25, SendMessageOptions.DontRequireReceiver);

    }else{

    if(sparksPrefab){

        Instantiate(sparksPrefab, hit.point, rot);

        }

    }

}

}

}

more ▼

asked Dec 13 '11 at 08:11 PM

Dreave gravatar image

Dreave
122 82 94 96

Please edit your post so that the whole of your code is in the code brackets, or nobody will read your code, and therefore, nobody will answer.

Dec 13 '11 at 09:50 PM Tasarran
Dec 14 '11 at 01:01 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Well, it looks like you have two scripts that aren't talking to each other.
Animations Script plays your SHOOT animation any time FIRE1 is pressed, regardless of whether or not you have any bullets left; It actually has no idea that there's even such a thing as bullets (aka : amountOfShots) .
You can either set up a chat-line between your scripts, or merge them into one script, or cut & splice this & that to here or there, or whatever... but whatever you do, you gotta let your FIRE1 command know that if your gun is empty then you don't want it to play the shoot animation.
It's kinda a pain to read when it's not in all in a code block like @Tasarran said, and variables thrown all over the place made it kinda weird to look at also. ... If I read it wrong, I read it wrong (went cross-eyed for a moment), but that's what I could make of it anyway.

Edit:


I suppose if you want to get dirty with it and make a quick change, you could also just add a bullet counter to your animation script.

var fullClip : int = 8 ;
var bulletsLeft : int = 8 ; //just make sure you always set these 2 variables to the same value as the amountOfShots variable in your other script: if you change one, change the others to match

if(Input.GetButtonDown("Fire1")){
   if(bulletsLeft > 0 ){
     bulletsLeft -- ;
     animation.Stop();
     animation.Play("shoot");
   }
}

if(Input.GetButtonDown("r")){
   animation.Stop();
   animation.CrossFade("reload");
   bulletsLeft = fullClip ;
}
more ▼

answered Dec 14 '11 at 12:53 AM

Lo0NuhtiK gravatar image

Lo0NuhtiK
3.5k 1 9 39

Thankyou for your help, sorry about the code I couldn't get it to format correctly.

Dec 14 '11 at 05:27 PM Dreave
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5093
x3807
x3747
x1535
x25

asked: Dec 13 '11 at 08:11 PM

Seen: 822 times

Last Updated: Dec 14 '11 at 05:28 PM