using UnityEngine; using System.Collections; public class animController : MonoBehaviour { public Animator anim; // Use this for initialization void Start () { anim = GetComponent(); } // Update is called once per frame void Update () { if (Input.GetKeyDown("1")) { anim.Play("cubeAnimation"); } if (Input.GetKeyDown("1")) { anim.Play("New cube Animation"); } } }
@spencer79856 жыл бұрын
What is the second if? the New cube Animation?
@praveenvinopv99296 жыл бұрын
@@spencer7985 The second if statement is when we press 1 again it start play new cube animation in this case it is idle animation by this way we can start or stop animation
@apersonthatsucksatnames11336 жыл бұрын
how do you make it stop when you let go?
@iamthewankerpants6 жыл бұрын
@@apersonthatsucksatnames1133 Asking the real questions here
@dz_apollo5 жыл бұрын
@@iamthewankerpants if (Input.GetKeyUp("1"))
@jakeshuman21027 жыл бұрын
literally an hour of forum reading didn't produce the quality of information this 6 minute video gave.
@CubicBrain7 жыл бұрын
Thank you very much Jake. I really appreciate it.
@bevel17026 жыл бұрын
Same!
@AGeekNamedRoss6 жыл бұрын
Guessing... Udacity forum?
@MrCornerMan6 жыл бұрын
ikr i was searching forever before i found this
@IvyTeaRN5 жыл бұрын
literally the same thing
@mindonwarp5 жыл бұрын
As simple as I needed it. Thank you! For all of you looking for a way to play the animation only once and go back to the initial state: just go to the animation editor UI ("Animator" tab), right click on your triggered animation node and choose "Make transition". An arrow coming out of the node will show up. Just pull this arrow onto your initial/idle state node and that should do it (did for me).
@M4SKD4 жыл бұрын
THANK YOU SO MUCH IVE BEEN TRYING TO WORK OUT HOW TO DO THIS FOR AGES :)
@JohnJohnson-ox3bu3 жыл бұрын
Thanks dude
@jenniferlight5050 Жыл бұрын
thank you sir!! You have saved my life. This was like attemp 50 of trying to get my door to open.
@xExplodingStarx7 жыл бұрын
thank you for being so clear and to the point, really needed that.
@CubicBrain7 жыл бұрын
Thanks a lot!
@sergioborrallo74244 жыл бұрын
I checked your profile, I see that you don't make videos anymore. I really regret it, I think you explain extremely well. Thank you for your effort and time!
@brealz53517 жыл бұрын
Incredible.. Fast and to the point, great tutorial !
@CubicBrain7 жыл бұрын
Thanks a lot!
@gabrielbuchala77987 жыл бұрын
The key to a good tutorial is to communicate in a clean, precise and efficient way. This video hit the nail on the god damn head. I'm sure this video is going to help a lot of people, keep up the good work.
@CubicBrain7 жыл бұрын
Thank you so very much. I really appreciate your comment. Its comments like these that really give me the drive to continue making these tutorials.
@r0y1t074 жыл бұрын
I like videos like this, no longer than 10 minutes and direct to the point :)
@xeon52593 жыл бұрын
this is a very outdated tutorial, but still works for me, thanks to your easy,simple, to the point explanation. ThankYou!
@007walk6 жыл бұрын
Thanks for this. This is a much simple way of implementing what i wanted. For some reason my animation kept transitioning to the previous state with when using the trigger, but this just saved me a bunch of time. Thanks again.
@CubicBrain6 жыл бұрын
Hi Rodane, thanks for your feedback, I'm glad you liked it.
@tristunalekzander56084 жыл бұрын
After several minutes of searching, this was exactly what I needed, thank you!
@oofusmcdoofus4 жыл бұрын
Me too
@ligrens4 жыл бұрын
Several hours you mean.
@Itay_Ron4 жыл бұрын
Thank you! after 4 years I could not find a better guide!
@ZekaProf5 жыл бұрын
well i was trying for ages to get my main menu animation to work with overwhelming solutions but yours was the simpliest and the most precise
@СтепанЛутошкин-о8ю4 жыл бұрын
You are the best! I was looking for the solution for 4 hours! Thank you so much!
@kristophia73104 жыл бұрын
i lost my sanity before i found him Psychological factors that may contribute to mental illness include: Severe psychological trauma suffered as a child, such as emotional, physical, or sexual abuse. An important early loss, such as the loss of a parent. Neglect. An important early loss, such as the loss of a parent. - button plays animation gone, Neglect. sources cant help i am only joking
@RUDERUDYTUTORIALS Жыл бұрын
Still helpful in 2023 thank you very much for your support.
@a-vena3 жыл бұрын
Finaly i found this Tutorial. Thanks
@sbdesigns49996 жыл бұрын
Great! I had been looking for a really direct and simple example and here it is! Thank you :)
@oviroysarker80664 жыл бұрын
You have saved my unity carrier!!!!!
@clintstapleton22895 жыл бұрын
This is exactly what I needed thank you!
@darkhackerstudio81124 жыл бұрын
How do you make it so that when you release the key it stops the animation
@Zukomazi5 жыл бұрын
Works like a charms Nice and simple
@ImGodz06 жыл бұрын
how can i stop an animation on releasing button and go back to idle animation in void update?
@RyanRioga6 жыл бұрын
Anyone who might find an issue where the animation will not trigger, Add (KeyCode.1) (I used 'Q' for mine) to the code after GetKeyDown. Other than that, super helpful video! Checked a lot of forums and whatnot and found nothing helpful. Nice job dude.
@CubicBrain6 жыл бұрын
Thank a lot, and good tip!
@modernistt6 жыл бұрын
used for my gun game. Thanks very quick and straight to the point.
@aksovicdalibor9985 жыл бұрын
+CubicBrain Do you, by any chance, remember what key combo u did at 4:40, to jump right into Start function? Looks super useful for bigger projects.. Thanks for video, helped a lot.
@animeboy936 жыл бұрын
I hope this one finally helps me with my animation troubles. Been trying to work this stuff out for days. I keep seeing videos where they set the Boolean or trigger and the animation just works.
@theotherone01017 жыл бұрын
how can i do it. if it just should play the animation 1 time ?
@CubicBrain7 жыл бұрын
Click the animation in the project field, and uncheck "Loop time", in the Inspector.
@100percentpodcasts77 жыл бұрын
Cheers
@official420er6 жыл бұрын
but after activating the animation once, i cant do it after that.. could this be solved?
@rajjaiswal16066 жыл бұрын
Zuchini DE sorry for late reply but in update use the following code If(Input.GetKeyUp("1")) ( anim.play("name of animation") = false ) If not use this one If(Input.GetKeyUp("1")) ( anim.stop("name of animation") ) Or use this one by making another animation which has nothing transformed and write If(Input.GetKeyUp("1")) ( anim.play("name of animation1") )
@official420er6 жыл бұрын
thanks alot
@AstrusHD5 жыл бұрын
Awesome Tutorial, it helped me so much. Thank you!
@RUDERUDYTUTORIALS Жыл бұрын
It's really helpful right.
@kapex64053 жыл бұрын
Awesome tutorial have had trouble with this for like a year or so :D
@kapex64053 жыл бұрын
Also this was super easy to customize
@leeli92435 жыл бұрын
thank you very much! It's nice and easy to understand
@og_shrug4 жыл бұрын
Man this vid helped me get into a club (coding C# and unity) . Thnx
@TheRealArtDoctor4 жыл бұрын
hey man i have one small issue the code error says aplay has no definiton
@alirazahaider71083 жыл бұрын
What if there were Different animations to be played on different objects then how will we be able to do that?
@AwsomeDragon6 жыл бұрын
Excellent tutorial, thank you so so so so much!!
@kieronrana52334 жыл бұрын
great video! straight to the point, literally that easy. thanks for your help :)
@tassodemo23164 жыл бұрын
If you press it once, it goes forever. If you turn off the loop, it only plays once.
@garlicbread15754 жыл бұрын
yes but how do you turn off the loop?
@AwsomeDragon6 жыл бұрын
how can i stop the animation?
@stasiek00796 жыл бұрын
You need create new animation
@rahulpatil33336 жыл бұрын
this.GetComponent().stop();
@AwsomeDragon6 жыл бұрын
rahul patil thank you 🙏
@shauryakumar3063 жыл бұрын
@CubicBrain Thanks you really showed me how it's done.
@BojanVasilev6 жыл бұрын
hello great tutorial but how can i make when i press again to animation stop and if i press again animation start again
@CubicBrain6 жыл бұрын
Hi Bojan, You will have to set up an if statement, that looks for the animation state (which animation is currently playing.)
@jirahmarienavarro87014 жыл бұрын
What if I just want it to rotate one time and it should disable any more movement?
@Yu0nes6 жыл бұрын
Thank you man very good tutorial
@CubicBrain6 жыл бұрын
Thank you very much.
@Normal_Norman6 жыл бұрын
Cheers, worked first time!
@tomcruz86157 жыл бұрын
Quick question. What would you use to trigger two animations with the same key? (Sounds like a dumb question but i'm a unity newb so whatever).
@kaldogorath7 жыл бұрын
I'm not sure if you can play two animations from the same controller (anim here) at the same time. Either way, you would simply call another thing in the if statement where the anim.Play(); is called already. so it would read like anim.Play("clip1"); anim.Play("clip2"); That is, if you can play two from the same one. If not, you'll need two animator controllers and would call anim1.Play and anim2.Play back to back in the if statement.
@tomcruz86157 жыл бұрын
Nick Miller ohhh I'll try both of those out. Thank you for that
@Nikoplays09105 жыл бұрын
Thanks, now I can play the orange justice animation
@nibernator6 жыл бұрын
For some reason I am getting an error: Invalid Layer Index '-1' I did everything exactly like the video, no dice...
@CubicBrain6 жыл бұрын
Hi, please double check your namings/case-sensitiviness for the animations etc. could you post your code?
@nibernator6 жыл бұрын
using System.Collections; using System.Collections.Generic; using UnityEngine; // THIS SCRIPT NOT WORKING public class animController : MonoBehaviour { public Animator anim; // Use this for initialization void Start () { anim = GetComponent(); // caches controller at start. } // Update is called once per frame void Update () { // string reference //check for key press if (Input.GetKeyDown("1")) { print("Key Down"); // To test if input responds to key. anim.Play("cubeAnimation"); // Plays the animation asset named cubeAnimation. } } }
@nibernator6 жыл бұрын
These are what the errors say, "Animator.GotoState: State could not be found UnityEngine.Animator:Play(String) animController:Update() (at Assets/animController.cs:22)" The other error: "Invalid Layer Index '-1' UnityEngine.Animator:Play(String) animController:Update() (at Assets/animController.cs:22)" Thanks for your time!
@CubicBrain6 жыл бұрын
I'm thinking it could be one of two things. It might be that the animator hasn't been added to the field in the inspector, or that the naming of the specific animation state (the animation you want to play back) isn't correct?
@karanveermultani65944 жыл бұрын
I know this comment was a year back but I found out what was wrong. There is an extra if statement in the update. Remove the one that plays "New cube animation" and you won't get the error anymore. The code in the comments has this, so please do mind changing it.
@leongrigoruk6 жыл бұрын
One of the best
@CubicBrain6 жыл бұрын
Thanks. appreciate it.
@Conmac6 жыл бұрын
PLEASE HELP! I have a running animation and an idl animation, once i press 1 to trigger another animation, after its done none of the other animations work? Help
@CubicBrain6 жыл бұрын
Hi Conmac. does any of your animation go to the exit state?
@Conmac6 жыл бұрын
CubicBrain I have figured it out! But thanks for responding bro
@CubicBrain6 жыл бұрын
okay, great.
@Dennis10Paulino7 жыл бұрын
Very simple and effective, thanks!
@CubicBrain7 жыл бұрын
Thanks man!
@praveenvinopv99296 жыл бұрын
Thank you brother you earned 1 subscribe i found many video regrading to this lesson but all the video was difficult and yours was just asweome keep it up and kindly keep us to know this kind of short tricks
@CubicBrain6 жыл бұрын
Thanks a lot. will do.
@seddaouiyassine78146 жыл бұрын
How if we only want it to play 1 time?
@onatunlu51586 жыл бұрын
Thanks dude It fixed my problems.
@fiQmeister5 жыл бұрын
what if i want to be able to stop and start t again?
@icantth1nkofanam404 жыл бұрын
mine only works once then it doesnt trigger after that
@sanjaychowdappa63065 жыл бұрын
Thanks a lot sir u have best solution
@jazminajelani83956 жыл бұрын
hye, i want to create a game where player will be given 2 button and if they click on wrong answer button there is animation will play and vice versa. but i dont know how to code the button to play the timeline because the animation is in the timeline. could u please help me?
@gabrielguitiánestrella6 жыл бұрын
succinct and precise, awesome!
@CubicBrain5 жыл бұрын
thanks!
@vicuna40366 жыл бұрын
How can i let it play one time and it will automaticly go back to emphty state?
@310thebank7 жыл бұрын
how about if we want our player collider with trigger box and play animation not press key button how we must change code
@alejandrozamora34685 жыл бұрын
Thank you, very helpful
@asklapios3 жыл бұрын
thank you so much about this training
@jasielt.20064 жыл бұрын
hey i dont know if you can help me but whenever i press the button to do the animation it stays frozen at the end of the animation and dosent transform back to its orignal state and says Katana AnimationEvent has no function specified
@CubicBrain4 жыл бұрын
Hi Jasiel, You can add a transition to an idle state in the animation, or make the animation loop. Animations are a bit like video playback, and will just stop at the last frame.
@emilyallen79122 жыл бұрын
It won’t let me add a new state when I right click it just says add behavior
@haru_ne_4 жыл бұрын
I wanted to know how to use Trigger parameters but I figure it out anyway. Thanks.
@ruler15595 жыл бұрын
say if I want to get back to the other animation, how do I do it?
@CubicBrain5 жыл бұрын
If you just want it to go back automatically, right click the animation node that you trigger, and select make transition. then click back on the first animation node.
@iTapClashRoyale7 жыл бұрын
Why doesn't Visual Studio recognize "Animator"???
@samu-rai9835 жыл бұрын
YEAAAAHH WHYYYYY THEE FFUUUUUCK NOOT
@ChuckyMaster5 жыл бұрын
thank you bro really help ful
@joshuaheejd7 жыл бұрын
I need some help with double keypress trigger animations? Meaning when i press e.g X, 1 thing happens and then when i press Y another thing happens to the same model. Are there any tutorials on this?
@JacksonAcademy17 жыл бұрын
Thank you~
@shaquielleemptage18887 жыл бұрын
How do I make it stop when I let go of the key
@CubicBrain7 жыл бұрын
Hi, You should be able to make a new if statement where you look for keyUp. A bit like this: if (Input.GetKeyUp("1")){ anim.Play("New State");}
@rajjaiswal16066 жыл бұрын
CubicBrain
@rajjaiswal16066 жыл бұрын
CubicBrain why didn't you reply to zuichini DE's comment? I just answered him 3 ways
@bratish92907 жыл бұрын
What about having more than one animation state? I can't find any information about this issue(
@historyofmistory74217 жыл бұрын
I have a Q why I can't scale my terrine when I use unity ?
@hal694206 жыл бұрын
How can I trigger it with OnTriggerEnter Please tell me.
@CubicBrain6 жыл бұрын
Hi Zeyad, You have to set up a trigger-collider on the trigger (gameobject) of your choosing. then on that object in a script use the "OnTriggerEnter" function like: void OnTriggerEnter(Collider other) { //code you want to execute } as soon as another gameobject with a colliger enters your trigger collider, it should execute the code.
@rickfuzzy6 жыл бұрын
Excellent. Feel like i just leveled up
@CubicBrain6 жыл бұрын
Awesome! happy that it helped you!
@bogdan_cx4 жыл бұрын
Thanks a lot man, you helped me so much!
@blzy23057 жыл бұрын
How to make this with Left Pressed?
@camy_dev7 жыл бұрын
I really loved this thanks man I used it with a trigger to make a script to play when I'm in a trigger, but I was wondering how I specify in the script that i only want it to play once, in other words i have a dresser that moves when I press e near it but i only want you to be able to play this animation once otherwise you can move the same dresser over and over again any help would be appreciated thank you so much already though.
@CubicBrain7 жыл бұрын
The simplest solution would be to set a transition within the animator, to either the exit state, or a new idle state.
@CubicBrain7 жыл бұрын
you can also check the reply to ewwitsantonio question answered by xXpandatree12Xx10. He added a nice three step guide.
@camy_dev7 жыл бұрын
Thank you got it lol
@thecomedyduo40014 жыл бұрын
how to I make the animation keep repeating until i let go of the key?(for walking)
@CubicBrain4 жыл бұрын
you might be able to use Input.GetKeyUp to stop the animation
@grosbarbu57785 жыл бұрын
It's possible to do the same thing with the E key ? If yes how to do that ?
@CubicBrain5 жыл бұрын
Hi Privelol, You should be able to just change the 1 inside the two double quotes to the desired letter.
@ミフヒヨ7 жыл бұрын
soo i can direct animation like that using while for example walk while pressing w ?
@toddlertech80917 жыл бұрын
great tutorial... Thanks
@CubicBrain7 жыл бұрын
Thanks man!
@deryabwaseem37936 жыл бұрын
how I rotate my object in the menu page in unity
@sakshamsahgal94206 жыл бұрын
can u plz tell me how to pause animation using this?
@CubicBrain6 жыл бұрын
Hi Saksham, There is no straight forward way to do it (anim.Pause() will not work). You might want to look into stopping the time or setting the speed of your animation. There is a thread on the unity site that discusses this issue: answers.unity.com/questions/48077/pausing-an-animation.html
@anandpattar31065 жыл бұрын
How to stop the animation?
@LeCigareVolant6 жыл бұрын
Did you ever know that you're my hero? -Bette Midler
@CubicBrain6 жыл бұрын
Hi. Thanks a lot.
@FoxGAMING-NTF6 жыл бұрын
Just a quick question... How do I make the animation play only once?
@CubicBrain6 жыл бұрын
Hi, You can set an "Exit" on the animation. You have your animation that you start with. Normally that it would then just sit there stuck in that animation. However if you set a transition to exit or an empty animation it will only play once.
@FoxGAMING-NTF6 жыл бұрын
CubicBrain Thanks!
@FoxGAMING-NTF6 жыл бұрын
CubicBrain Also, I need help with something else. . . So, I'm making a horror game. There is a fridge that will contain clues. It has double doors. I want to add the animation to it. How would I make it so that when I am close to the doors (Each door has a box collider, and if the door rotates, the box collider rotates with it) another animation plays (aka the closing)? How could I switch the activator to be a left click (GetMouseDown;), right? And how could I make it so that When I left click, it Opens, and stays open, even when I walk away, and when I walk back into the box collider, how could I make it so that I have to click again to close it? Sorry for all of the questions, I hope they make sense and you can help me figure them out. I'm just desperate to get this done, and nothing else has helped...
@hyenagametalk72937 жыл бұрын
how would I get I to stop when I release the button
@lucutes29364 жыл бұрын
how do I make them movable with WASD
@kkrazius6 жыл бұрын
Thank you, it worked just fine!
@jimmsdesign-freelancegraph15495 жыл бұрын
I was hoping you may be able to help. I followed your tut but tailored one or 2 bits but I'm getting this error - MissingComponentException: There is no 'Animator' attached to the "Player" game object, but a script is trying to access it. You probably need to add a Animator to the game object "Player". Or your script needs to check if the component is attached before using it. - I have attached the script to both the player and the object but it still throwing back this error. Do you know what I could be doing wrong?
@joshuamason22275 жыл бұрын
attach animator
@vlad-cristiansecatureanu68205 жыл бұрын
I want to make it so that you can press the button multiple times a knob moves further like one step at a time. Is it posible to do it with only one animation, or do i need one for each step of the knob? pls reply
@CubicBrain5 жыл бұрын
Hi, well you could play a part of the animation, but that setup would probably be a bit complex. if you create a bunch of takes/animation you could play them back and use basic if statement setup to see how far along the line you are. however if its simple movement you are going for. I would suggest just scripting the movement of the gameobject. this post might help you: answers.unity.com/questions/667641/how-do-i-move-my-2d-object-using-arrow-keys-also-h.html
@v3vlog6 жыл бұрын
hey I am not seeing empyt in animation controller
@Jaydanson7 жыл бұрын
using System.Collections; using System.Collections.Generic; using UnityEngine; public class animcontroller : MonoBehaviour { public Animator anim; // Use this for initialization void Start () { anim = GetComponent(); } // Update is called once per frame void Update () { if (Input.GetMouseButtonDown("0")) anim.Play("ReadSwing01") } That's what the script is supposed to look like right? Because i keep getting errors
@CubicBrain7 жыл бұрын
You need closing } for tour update method and class (two of them at the end of your script).
@HDMaco136 жыл бұрын
You need to remove the -> " " in if (Input.GetMouseButtonDown("0")) It should look like if (Input.GetMouseButtonDown(0))
@silvanacallegari7 жыл бұрын
you are AWESOME!
@CubicBrain7 жыл бұрын
Thank a lot.
@redinternet98936 жыл бұрын
why my rotate animation is fast > slow > fast > slow (loop)
@CubicBrain6 жыл бұрын
Hi Ken, You need to select the keyframes in your animation, and set them to linear. this way the wont do bezier/easing animations.
@redinternet98936 жыл бұрын
where can set linear
@CubicBrain6 жыл бұрын
In the "Animation" window, with your gameobject selected you should be able to see the Rotation (like in the video at 2:30). if you select the keyframes, and right click then, in the drop down menu you can change the "Tangents". if you choose "both tangents" and set them to "Linear", you animation should be smooth.
@ashoan89657 жыл бұрын
What about uh, this is for my car game... I want a door to open and stop on the press of a key, and if pressed a second time, it reverses, the door would close, like if the animation is reversed and stop like it did when opening the door. hood trunk it could be anything.
@drgitgud6 жыл бұрын
@CubicBrain hello i´ve googeled for like 2 days now but i cant find any answers so i may bother you ^^ im looking for an animation on an instantiated object, my problem is as i set the animation up (just going up on the y nothing more) it takes the x and z axes aswell and my onmouse instantiate doesnt work anymore. the object doesnt spawn where i click instead all the clones spawn at the set up x and z values. i want to make a spell where the player raises a wall from the ground kinda like earthbender styled but i cant get it to work. thank you so much in advance if you actually answer it would make my day :) peace
@CubicBrain6 жыл бұрын
Hi, I'm not sure how you would achieve the functionality that you are looking for. but, have you tried posting on the Unity3D forums? They are always willing to help.
@drgitgud6 жыл бұрын
i had to place my wall into an empty gameobject to ignore its transform. my object was spawning right where animated it in the scene so it took the x and z values with it to avoid using them parent it. thanks :)
@juankacoolx05 жыл бұрын
You are the best one
@berkaybakacak7 жыл бұрын
Thnaks dude. You are great :)
@CubicBrain7 жыл бұрын
Thanks, glad you liked it.
@hazestorm14675 жыл бұрын
idk if u here at all but i got a lil problem. so this code works great. but. i want the animation played once. so i turn off loop. but than i realize. i can only activate the animation once and not every time i press the key? help TT
@CubicBrain5 жыл бұрын
Hi, if you make an empty animation in your animator, and set a transition to that state, the animation should play once, and can then be activated again.