- Home /
Unity animations not working
hi, I have tried many times to get animations to play with my gun when i press certain buttons. I have imported my gun from blender, and have made the animations on unity using the animation window. I have tried 2 different types of animation script so far, and none have worked.
First Script:
function Update()
{ if(Input.GetMouseButtonDown(0))
{ // Plays the fire1 animation - stops all other animations
animation.Play("Shoot", PlayMode.StopAll); } }
Second Script:
if(Input.GetKeyDown("Fire1") && !animation.IsPlaying("Shoot"))
{ animation.Play("Shoot"); }
Also when i play game, the animation for walking starts automatically without any scripts. Is there a way to stop this? And why won't my animations work? Please help. Thanks
The modern animation techinques dont make sense to me. I usually just set animations of an imported Blender model to Legacy. and then just activate things manually from code.
Answer by Sarthak123 · Oct 23, 2014 at 10:43 AM
i think you have set animation of walking as default animation in your player object, you should set it as a animation, not a default animation...please re-check the inspector..
Second thing, make your script like this.
function Update()
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
animation.Play("Shoot");
}
}
and check your inspector and make sure Shoot animation is present in you animation list or not.
Check in the console for the errors generated during run-time...
hope this helps..
How do i add animations to the object? Once i drag one in, the next one i drag in gets over written.
actually you are using animator component rather than the animation component, unity uses 2 types of animations, first the simple basic animations which are controlled by animation component and second mecanim or complex animations controlled by a avatar and a animator compnent(you are tending to use this), further details you can find at unity's website, tutorials>animations and docs, in other way first delete this animator component first than click in "Select" button situated just below the name and tag options in the inspector and then go to Rig and then in the animation type select legacy..
then delete the animations of this character and its avatar and now go to add component, miscelaaneous, and then select animation, the one with play button sign, now re-record your animations, and then these scripts will work
I always do this, it may loo hard but is extremely easy once you learn the concept, please check the unity's tutorial to get in depth with this process,
Hope it helps and please reply if you have any other problems,
please accept it as correct answer if you think it helped..:)
Answer by RPMonkey · Oct 26, 2014 at 08:55 AM
What do you mean by re-cording my animations? Does it mean to make it all again? Its says my scripts are trying to access 'animation'. I've been able to set the gun to legacy, and add all my animations into the play button component.
Thanks for all the help and it is really appreciated!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
what's wrong with this script? 2 Answers
Scripting help 2 Answers
My player won't jump? 1 Answer
Need help with some OnTrigger Scripting 2 Answers