- Home /
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);
}
}
}
}
}
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.
Answer by Lo0NuhtiK · Dec 14, 2011 at 12:53 AM
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 ; }
Thankyou for your help, sorry about the code I couldn't get it to format correctly.
Your answer
Follow this Question
Related Questions
adding animation dynamically by script 0 Answers
C# Check Physics.Raycast Once 0 Answers
Animation Script Help 0 Answers
How to keep an animation attached to gameobject 0 Answers
Create light shapes on plane using C# (similar to the game Closure) 2 Answers