- Home /
Stop looping animation?
My script tells my animated gun to disable the shooting animation after it has been played 30 times, this works fine except my gun is fully automatic. This means I want my script to know when my animation has looped 30 times instead of being played only once. This is my script, I hope I explained okay...
function Start () {
animation["reload"].speed = 2;
animation["shoot"].speed = 10;
animation["shoot"].wrapMode = WrapMode.Loop;
animation["reload"].wrapMode = WrapMode.Once;
animation["shoot"].layer = 1;
animation["reload"].layer = 2;
animation.Stop();
StartCoroutine(ShotPoller());
}
function ShotPoller () {
while(true){
if(Input.GetButton("Fire1")){
Shoot();
yield WaitForSeconds(refireRate);
}else{
animation.Stop("shoot");
}
if(Input.GetKeyDown("r")){
Reload();
}
yield;
}
}
function Shoot (){
if(Input.GetButton("Fire1")){
if(bulletsLeft > 0 ){
bulletsLeft -- ;
animation.Stop();
animation.Play("shoot");
}else{
animation.Stop();
}
}
}
var fullClip : int = 30;
var bulletsLeft : int = 30;
var waitTime = 2.5;
var refireRate : float = 0.1;
function Reload () {
if(Input.GetButtonDown("r")){
animation.Play("reload");
yield WaitForSeconds(waitTime);
bulletsLeft = fullClip ;
}
}
I wander why you want to know the number of animations loop.
I'm guessing you want to stop it when you run out of ammo.
You should just check if you have more then zero ammo and only then play the animation.
hmmm... now that I look at your code again, it seems you are doing it allready.
if(Input.GetButtonDown("Fire1")){
if(bulletsLeft > 0 ){
bulletsLeft -- ;
animation.Stop();
animation.Play("shoot");
}
}
Are you saying that the animation is still playing after your bulletsLeft is 0?
No the animation stops if it is at 0 but the number only goes down by one every time you click the left mouse button, I want it to continuesly go down until you stop holding the mouse button.
Answer by dorpeleg · Mar 17, 2013 at 10:03 PM
Use GetKey insted of using GetButtonDown.
Description
Returns true while the user holds down the key identified by name. Think auto fire.
Now I have my number going down continuesly but my guns animation doesn't stop once it gets to 0 any ideas? I also updated my script since I have changed it a bit.
Try this:
if(Input.GetButton("Fire1")){
if(bulletsLeft > 0 ){
bulletsLeft -- ;
animation.Stop();
animation.Play("shoot");
}else{
animation.Stop();
}
}
Once the number gets to zero the animation stops but If I stop holding the mouse button when the ammo is above 0 it keeps playing the animation.
Add this:
if (Input.GetButtonUp ("Fire1")) {
animation.Stop();
}
I figured it out and updated my code, thanks for your help!
Your answer
Follow this Question
Related Questions
3rd person gta like weapon problem?? 0 Answers
Animation loop stop? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Animation On Mouse Click 1 Answer
Loop Animation In Script? 2 Answers