- Home /
how to make a animation play only if you press a certain key?
I have a ak 47 gun model with an reloading animation. I dont want the reloading animaation to start right when i start playing my game. I only want my gun to start reloading when i press the key "r". How do i do this? Do I need some sort of script or something?
Answer by Jesus_Freak · Oct 24, 2010 at 04:57 PM
well, a script would work, it would be something like:
function Update()
{
if(Input.GetKeyDown("r"))
{
// Plays the reload animation - stops all other animations
animation.Play("reload", PlayMode.StopAll);
}
}
just so you know, you have to rename the "reload" animation to whatever you called it.
hope this helps.
just go to Edit --> Project Settings --> Input, define the "r" key or whatever key you want and combine that with the answer above. You should have smooth sailing from there
actually, Get$$anonymous$$eyDown becomes true whenever you press down on the keyboard key that you define, i gave you the r key, so when you press r, and if you set the variables, the reload animation should play. what Proclyon said would work with the GetButtonDown statement. but you don't HAVE to do that with the Get$$anonymous$$eyDown statement.
can u please say how to put the variables and where, cuz this doesn't work, thank you =)
Answer by sam 1 · Nov 07, 2010 at 04:27 PM
well guys this is what i did. i made the script and made a new input. I made a new javascript, called "reload", and copyed and pasted that script you gave me into it. Then copyed that script into my gun item thing. currently its in the hierarchy. my gun is also called "reload". i tried it and it didnt work. it kept saying: "the animtaion state reload could not be played because it couldnt be found!" so i tried the iput thing. i made a new input called reload. i put the postive key as "r". i tried it again and it didnt work. it said the same thing. "the animtaion state reload could not be played because it couldnt be found!" whats wrong? why cant it play it? why does it keep saying that? could some one help me?
Answer by Dummy7307 · Feb 04, 2011 at 03:25 PM
Do as Unity3_User says but change (Input.GetKeyDown) to (Input.GetButtonDown)
Answer by joe_coronado · Feb 01, 2012 at 09:12 PM
This happened to me it was because the animation name had a capital ' R'and the "reload" in the script doesnt. so check your capitals, this answer is for sam
Answer by DevDale · Mar 03, 2012 at 10:37 PM
How can I make this same affect apply for when I press a mouse button?
For left mouse button: if (Input.Get$$anonymous$$ouseButtonDown(0)) {
For right mouse button: if (Input.Get$$anonymous$$ouseButtonDown(1)) {
For middle mouse button: if (Input.Get$$anonymous$$ouseButtonDown(2)) {
There you go, $$anonymous$$lep
Your answer
Follow this Question
Related Questions
Play animation when 2 keys pressed? 3 Answers
Play animation in child model 0 Answers
Play a video on clicking on an object 1 Answer
On collision play animation 1 Answer
Retrieving the name of the animation currently playing 9 Answers