- Home /
How to run an animation clip on button down-hold?
Hi I am working on a fps shooter game, and im new to coding so I have no idea how to do this. What I need to do is run an animation on button down click and if the person holds the "w" key down the animation will continually until he lets go(button up). Its for the leg animation in my fps game, I want it so when the player holds down the "w" key he can look down and see the legs running while he's running. Thanks
Answer by clunk47 · Dec 15, 2012 at 03:36 AM
I'm assuming you have your animations / frames setup already. Here is a SIMPLE EXAMPLE in C# to get you started.
if(Input.GetKey(KeyCode.W))
animation.Play();
else if(Input.GetKeyUp(KeyCode.W))
animation.Stop();
You will need to have basic coding skills to use this correctly. If in C# you need to have it inside a class... For more information look at the Unity Script Reference for Animations
Thank you thats exactly what I needed, is there anyway you could add four variables to that, because I want when the player clicks W S D A they will all play there animation which is the leg moving animation, A will be the left walking animation D will be the right W will be forward and S will be the backward walking which they will all have there own, I would, but like I sed before im not very good so I don't know how, plus the little bit I know is java, Thanks.
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
animation.Play("forwardWalk");
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
animation.Play("backPedal");
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
animation.Play("leftStrafe");
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
animation.Play("rightStrafe");
else if(!Input.any$$anonymous$$ey))
animation.Play("idle");
If you don't have an "idle" animation, just use animation.Stop(); If you want a smooth transition, you would use animation.CrossFade("animationName", fade time). But this will get you started at the very least. Also take a look at the Unity Script Reference for Animation
Your answer
Follow this Question
Related Questions
How to click a 3d object in unity3d? 3 Answers
Scores/points when you destroy something? 1 Answer
problem with weapon animation -.- 0 Answers
Problem with First Person Shooter Script 1 Answer
Weapon Possition Move On Sprint 1 Answer