c# - Control animation and execute script when animation ends -
my project military fps , i'm having problems animations.
i have 3 different weapons, 1 animator controller each 1 , every weapon has "enter" , "leave" animation. cs, cod, etc...
i need know when "leave" animation ends disable gameobject, enable other 1 , play "enter" animation.
i tryed this: http://answers.unity3d.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html without sucess.
i'll leave here print of animator controller, hierarchy , script, if u need more details, need say.
animator controller of weapon number 1
all transitions "sair" (leave animation) have trigger (ak47_sair) , transition "extit" state have trigger ("ak47_saircontrolador")
on code, when press 2 (change weapon number 2) want transition.
this hierarchy, script attached "jogador".
with actual code, disable tha ak47 gameobject when leave animation still playing.
using unityengine; using system.collections; public class firstperson : monobehaviour { public float speed; public float normalspeed = 5.0f; public float slowspeed = 2.5f; public float crchspeed = 2.5f; private transform tr; private float dist; // distance ground public float mousesensitivity = 5.0f; public float verticalrotation = 0.0f; public float updownrange = 60.0f; private float verticalspeed = 0.0f; public float jumpspeed = 5.0f; charactercontroller player; private gameobject ak47; private gameobject faca; public float shootingrate = 0.15f; public float shootcooldown; private bool agachado = false; public float camoriginalpositiony; public float camcrouchpositiony; private animator controladoranimacaoak; private animator controladoranimacaofaca; public capsulecollider playercollider; public camera cameraprincipal; public int armaselecionada; public int ultimaarma; void start () { player = getcomponent<charactercontroller>(); shootcooldown = 0; controladoranimacaoak = player.getcomponentinchildren<animator>(); playercollider = gameobject.getcomponent<capsulecollider> (); cameraprincipal = camera.main; armaselecionada = 1; ak47 = cameraprincipal.transform.findchild ("ak47_final_animado").gameobject; } void update () { if (input.getkeydown (keycode.alpha2) || input.getkeydown(keycode.keypad2)) { ultimaarma = armaselecionada; armaselecionada = 2; if(ultimaarma == 1) { controladoranimacaoak.settrigger("ak47_sair"); controladoranimacaoak.settrigger("ak47_saircontrolador"); ak47.setactive (false); } } if (input.getkeydown (keycode.alpha1)) { ultimaarma = armaselecionada; armaselecionada = 1; // controladoranimacaoak.settrigger ("ak47_entrar"); } if (armaselecionada == 1) { // diz ao controlador da anim se o player esta movimentar-se ou nao controladoranimacaoak.setfloat ("ak47_deslocacao", player.velocity.magnitude); //debug.log (player.velocity.magnitude); // dispatar tiros playershoot playershootscript = player.getcomponent<playershoot> (); if (shootcooldown > 0) { shootcooldown -= time.deltatime; } if (input.getbutton ("fire1")) { if (shootcooldown <= 0) { shootcooldown = shootingrate; playershootscript.fireshoot (); // animaƧao controladoranimacaoak.setbool ("ak47_disparar", true); } } else { // animaƧao controladoranimacaoak.setbool ("ak47_disparar", false); } if (input.getkeydown (keycode.r)) { controladoranimacaoak.settrigger ("ak47_rec"); } } } }
in unity 5 you've got new thing animation state machine behavior : state machine behaviours
you can use specify behavior when animation controller enters or leave specific states.
for exemple here i've got door have open , close state, , let's want play sound when door opening.
here clicked opening, add behaviour , set random name test (behavior test in case)
then need implement function void onstateenter(animator animator, animatorstateinfo stateinfo, int layerindex) play sound @ first frame animation running.
[serializefield] audioclip open_sound; override public void onstateenter(animator animator, animatorstateinfo stateinfo, int layerindex) { animator.getcomponent<audiosource>().clip = open_sound; animator.getcomponent<audiosource>().play(); }
in case, want implement behavior in state disparar implements function onstateexit([...]) , handle weapon change.
to go bit further don't think should handle weapon change directly in animation state, maybe script send event catched game controller handle change of weapon.
Comments
Post a Comment