I finded a solution
Wait for animation end before continue script JS
Hi, first I want to say you I searched in all same topic and question and I didn't find an answer so please don't give me this kind of answer :
http://lmgtfy.com/?q=unity+3d+wait+for+animation+finish
or
Hey, here's a lot of same question : http://answers.unity3d.com/questions/134960/finish-animation-before-continuing-script.html http://answers.unity3d.com/questions/336333/wait-for-animation-to-finish.html http://answers.unity3d.com/questions/964586/waiting-for-animation-to-finish-playing.html
...
So, I'm making an FPS, and I want to my player can't shot or anything else (exept walking) if he is reloading. My script :
#pragma strict
///////////////////////////////////////////////////////////////////VARIABLES///////////////////////////////////////////////////////////////////////
var bulletCasing : Rigidbody;
var ejectSpeed : int = 100;
var fireRate : float = 0.5;
private var nextFire : float = 0.0;
private var fullAuto = false;
var clip : int = 30;
var maxclip : int = 30;
var reserve : int = 300;
var minreserve : int = 0;
var shotsound : AudioClip;
var reloadsound : AudioClip;
var MunMax : boolean = true;
var reloadsoundplay : boolean = false;
var eject : Transform;
var anim : Animator;
var isShooting : boolean = false;
var isReloading : boolean = false;
///////////////////////////////////////////////////////////////////FONCTION UPDATE////////////////////////////////////////////////////////////////
function Start () {
anim = GetComponent(Animator);
}
function Update () {
// SECTION DE TIR
if(Input.GetButton("Fire1") && Time.time > nextFire){
if(clip >= 1){
nextFire = Time.time + fireRate;
var bullet : Rigidbody;
bullet = Instantiate(bulletCasing, eject.position, eject.rotation);
clip -= 1;
GetComponent.<AudioSource>().PlayOneShot(shotsound);
animation.Play("shot");
bullet.velocity = eject.TransformDirection(Vector3.left * ejectSpeed);
}
}
//SECTION DE CADENCE DE TIR
if(Input.GetKeyDown("v")){
fullAuto = !fullAuto;
}
//SECTION DE RECHARGE
if(Input.GetKeyDown("r")){
if(reloadsoundplay == true){
GetComponent.<AudioSource>().PlayOneShot(reloadsound);
animation.Play("reload");
}
if(reserve > 30){
RemoveReserve();
clip += maxclip - clip;
}
if(reserve < 30){
clip += reserve;
RemoveReserve();
}
}
//CHANGEMENT DE CADENCE DE TIR
if(fullAuto == true){
fireRate = 0.1;
}else{
fireRate = 0.5;
}
//BLOQUER LA RESERVE A 0
if(reserve <= 0){
reserve = 0;
}
//JOUER LE SON UNIQUEMENT QUAND LE CHARGEUR ACTUEL N'EST PAS COMPLET
if(clip == maxclip){
reloadsoundplay = false;
}
if(clip < maxclip){
reloadsoundplay = true;
}
}
///////////////////////////////////////////////////////////////////FUNCTION ON GUI///////////////////////////////////////////////////////////////////////
function OnGUI(){
GUI.Box(Rect(10,10,130,25), clip+ " / " +reserve);
}
///////////////////////////////////////////////////////////////////FUNCTION REMOVE RESERVE///////////////////////////////////////////////////////////////
function RemoveReserve(){
reserve -= maxclip - clip;
}
So you found a solution, good for you. Now can you share that solution, so that maybe the community can use the information?
Answer by Asgardr · Nov 08, 2015 at 12:25 PM
You could use a co-routine that yields for the length of the animation. An example is in the docs:
http://docs.unity3d.com/ScriptReference/AnimationClip-length.html
var anim: Animation;
function Start() {
anim = GetComponent.<Animation>();
anim.Play();
// Wait for the animation to finish.
yield WaitForSeconds(anim.clip.length);
}
You could then do something like setting a bool isAllowedToFire. Set it to false if the animation is playing, and true if the animation stopped. Something like the following:
var anim: Animation;
var isAllowedToFire: boolean;
function Fire() {
isAllowedToFire = false;
anim = GetComponent.<Animation>();
anim.Play();
// Wait for the animation to finish.
yield WaitForSeconds(anim.clip.length);
isAllowedToFire = true;
}
I don't really understand sorry, I always have problem with couroutine I don't understnd how it work.