Unity3D - Trigger animations (keypress trigger)

We take a look at how to activate the animation on a simple unity cube.

Пікірлер: 314

  • @CubicBrain
    @CubicBrain5 жыл бұрын

    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"); } } }

  • @spencer7985

    @spencer7985

    5 жыл бұрын

    What is the second if? the New cube Animation?

  • @praveenvinopv9929

    @praveenvinopv9929

    5 жыл бұрын

    @@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

  • @apersonthatsucksatnames1133

    @apersonthatsucksatnames1133

    5 жыл бұрын

    how do you make it stop when you let go?

  • @iamthewankerpants

    @iamthewankerpants

    5 жыл бұрын

    @@apersonthatsucksatnames1133 Asking the real questions here

  • @dz_apollo

    @dz_apollo

    5 жыл бұрын

    @@iamthewankerpants if (Input.GetKeyUp("1"))

  • @jakeshuman2102
    @jakeshuman21027 жыл бұрын

    literally an hour of forum reading didn't produce the quality of information this 6 minute video gave.

  • @CubicBrain

    @CubicBrain

    6 жыл бұрын

    Thank you very much Jake. I really appreciate it.

  • @bevel1702

    @bevel1702

    6 жыл бұрын

    Same!

  • @AGeekNamedRoss

    @AGeekNamedRoss

    6 жыл бұрын

    Guessing... Udacity forum?

  • @MrCornerMan

    @MrCornerMan

    6 жыл бұрын

    ikr i was searching forever before i found this

  • @IvyTeaRN

    @IvyTeaRN

    5 жыл бұрын

    literally the same thing

  • @mindonwarp
    @mindonwarp4 жыл бұрын

    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).

  • @M4SKD

    @M4SKD

    4 жыл бұрын

    THANK YOU SO MUCH IVE BEEN TRYING TO WORK OUT HOW TO DO THIS FOR AGES :)

  • @JohnJohnson-ox3bu

    @JohnJohnson-ox3bu

    3 жыл бұрын

    Thanks dude

  • @xExplodingStarx
    @xExplodingStarx7 жыл бұрын

    thank you for being so clear and to the point, really needed that.

  • @CubicBrain

    @CubicBrain

    7 жыл бұрын

    Thanks a lot!

  • @brealz5351
    @brealz53517 жыл бұрын

    Incredible.. Fast and to the point, great tutorial !

  • @CubicBrain

    @CubicBrain

    7 жыл бұрын

    Thanks a lot!

  • @ZekaProf
    @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

  • @r0y1t07
    @r0y1t073 жыл бұрын

    I like videos like this, no longer than 10 minutes and direct to the point :)

  • @gabrielbuchala7798
    @gabrielbuchala77986 жыл бұрын

    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.

  • @CubicBrain

    @CubicBrain

    6 жыл бұрын

    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.

  • @007walk
    @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.

  • @CubicBrain

    @CubicBrain

    6 жыл бұрын

    Hi Rodane, thanks for your feedback, I'm glad you liked it.

  • @sbdesigns4999
    @sbdesigns49995 жыл бұрын

    Great! I had been looking for a really direct and simple example and here it is! Thank you :)

  • @itay7884
    @itay78843 жыл бұрын

    Thank you! after 4 years I could not find a better guide!

  • @clintstapleton2289
    @clintstapleton22895 жыл бұрын

    This is exactly what I needed thank you!

  • @sergioborrallo7424
    @sergioborrallo74243 жыл бұрын

    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!

  • @tristunalekzander5608
    @tristunalekzander56084 жыл бұрын

    After several minutes of searching, this was exactly what I needed, thank you!

  • @oofusmcdoofus

    @oofusmcdoofus

    4 жыл бұрын

    Me too

  • @rukasugaberu4792

    @rukasugaberu4792

    4 жыл бұрын

    Several hours you mean.

  • @jenniferlight5050
    @jenniferlight5050 Жыл бұрын

    thank you sir!! You have saved my life. This was like attemp 50 of trying to get my door to open.

  • @modernistt
    @modernistt6 жыл бұрын

    used for my gun game. Thanks very quick and straight to the point.

  • @kieronrana5233
    @kieronrana52334 жыл бұрын

    great video! straight to the point, literally that easy. thanks for your help :)

  • @user-sp5ho7vy8l
    @user-sp5ho7vy8l4 жыл бұрын

    You are the best! I was looking for the solution for 4 hours! Thank you so much!

  • @kristophia7310

    @kristophia7310

    4 жыл бұрын

    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

  • @animeboy93
    @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.

  • @AwsomeDragon
    @AwsomeDragon5 жыл бұрын

    Excellent tutorial, thank you so so so so much!!

  • @AstrusHD
    @AstrusHD4 жыл бұрын

    Awesome Tutorial, it helped me so much. Thank you!

  • @RUDERUDYTUTORIALS

    @RUDERUDYTUTORIALS

    9 ай бұрын

    It's really helpful right.

  • @Zukomazi
    @Zukomazi5 жыл бұрын

    Works like a charms Nice and simple

  • @onatunlu5158
    @onatunlu51586 жыл бұрын

    Thanks dude It fixed my problems.

  • @RUDERUDYTUTORIALS
    @RUDERUDYTUTORIALS9 ай бұрын

    Still helpful in 2023 thank you very much for your support.

  • @Normal_Norman
    @Normal_Norman6 жыл бұрын

    Cheers, worked first time!

  • @xeon5259
    @xeon52593 жыл бұрын

    this is a very outdated tutorial, but still works for me, thanks to your easy,simple, to the point explanation. ThankYou!

  • @310thebank
    @310thebank6 жыл бұрын

    how about if we want our player collider with trigger box and play animation not press key button how we must change code

  • @leeli9243
    @leeli92435 жыл бұрын

    thank you very much! It's nice and easy to understand

  • @Dennis10Paulino
    @Dennis10Paulino6 жыл бұрын

    Very simple and effective, thanks!

  • @CubicBrain

    @CubicBrain

    6 жыл бұрын

    Thanks man!

  • @joshuaheejd
    @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?

  • @praveenvinopv9929
    @praveenvinopv99295 жыл бұрын

    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

  • @CubicBrain

    @CubicBrain

    5 жыл бұрын

    Thanks a lot. will do.

  • @oviroysarker8066
    @oviroysarker80663 жыл бұрын

    You have saved my unity carrier!!!!!

  • @derkenek7840
    @derkenek78403 жыл бұрын

    Finaly i found this Tutorial. Thanks

  • @shauryakumar306
    @shauryakumar3063 жыл бұрын

    @CubicBrain Thanks you really showed me how it's done.

  • @kapex6405
    @kapex64052 жыл бұрын

    Awesome tutorial have had trouble with this for like a year or so :D

  • @kapex6405

    @kapex6405

    2 жыл бұрын

    Also this was super easy to customize

  • @og_shrug
    @og_shrug3 жыл бұрын

    Man this vid helped me get into a club (coding C# and unity) . Thnx

  • @BogdanAndreiRO
    @BogdanAndreiRO3 жыл бұрын

    Thanks a lot man, you helped me so much!

  • @ImGodz0
    @ImGodz05 жыл бұрын

    how can i stop an animation on releasing button and go back to idle animation in void update?

  • @user-vj2ek5pz9s
    @user-vj2ek5pz9s7 жыл бұрын

    soo i can direct animation like that using while for example walk while pressing w ?

  • @Yu0nes
    @Yu0nes6 жыл бұрын

    Thank you man very good tutorial

  • @CubicBrain

    @CubicBrain

    6 жыл бұрын

    Thank you very much.

  • @jirahmarienavarro8701
    @jirahmarienavarro87013 жыл бұрын

    What if I just want it to rotate one time and it should disable any more movement?

  • @Nikoplays0910
    @Nikoplays09105 жыл бұрын

    Thanks, now I can play the orange justice animation

  • @Skullcat2212
    @Skullcat22124 жыл бұрын

    How can i trigger an animation when pressing a hud button?

  • @sanjaychowdappa6306
    @sanjaychowdappa63065 жыл бұрын

    Thanks a lot sir u have best solution

  • @jazminajelani8395
    @jazminajelani83955 жыл бұрын

    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?

  • @bratish9290
    @bratish92907 жыл бұрын

    What about having more than one animation state? I can't find any information about this issue(

  • @asklapios
    @asklapios3 жыл бұрын

    thank you so much about this training

  • @alirazahaider7108
    @alirazahaider71082 жыл бұрын

    What if there were Different animations to be played on different objects then how will we be able to do that?

  • @darkhackerstudio8112
    @darkhackerstudio81124 жыл бұрын

    How do you make it so that when you release the key it stops the animation

  • @alejandrozamora3468
    @alejandrozamora34685 жыл бұрын

    Thank you, very helpful

  • @SavanaStudios
    @SavanaStudios7 жыл бұрын

    Helped alot! Thanks!

  • @mehulsinghal4155
    @mehulsinghal41555 жыл бұрын

    hey ! i m not able to do the second part of the video...like when i click one the cube isnt rotating Can anyone please help me regarding that i copied the exact same code

  • @marciux3883
    @marciux38834 жыл бұрын

    can i make that after i pressed 1 animation starts and auto stops?

  • @aksovicdalibor998
    @aksovicdalibor9984 жыл бұрын

    +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.

  • @Mr09shaggy
    @Mr09shaggy6 жыл бұрын

    Thanks for the video :)

  • @vicuna4036
    @vicuna40365 жыл бұрын

    How can i let it play one time and it will automaticly go back to emphty state?

  • @historyofmistory7421
    @historyofmistory74216 жыл бұрын

    I have a Q why I can't scale my terrine when I use unity ?

  • @haru_ne_
    @haru_ne_3 жыл бұрын

    I wanted to know how to use Trigger parameters but I figure it out anyway. Thanks.

  • @darkexior
    @darkexior5 жыл бұрын

    succinct and precise, awesome!

  • @CubicBrain

    @CubicBrain

    5 жыл бұрын

    thanks!

  • @deryabwaseem3793
    @deryabwaseem37935 жыл бұрын

    how I rotate my object in the menu page in unity

  • @RyanRioga
    @RyanRioga5 жыл бұрын

    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.

  • @CubicBrain

    @CubicBrain

    5 жыл бұрын

    Thank a lot, and good tip!

  • @Shardey
    @Shardey6 жыл бұрын

    Assets/animController.cs(19,19): error CS1502: The best overloaded method match for `UnityEngine.Input.GetMouseButtonDown(int)' has some invalid arguments

  • @TheRealArtDoctor
    @TheRealArtDoctor3 жыл бұрын

    hey man i have one small issue the code error says aplay has no definiton

  • @hyenagametalk7293
    @hyenagametalk72937 жыл бұрын

    how would I get I to stop when I release the button

  • @hackersitoxd9689
    @hackersitoxd96894 жыл бұрын

    GREAT TUTORIAL!

  • @fiQmeister
    @fiQmeister4 жыл бұрын

    what if i want to be able to stop and start t again?

  • @AzoozePvP
    @AzoozePvP4 жыл бұрын

    Thanks Bro, but i have problem i use void OnTriggerEnter () { text.SetActive (true); if (Input.GetKeyDown(KeyCode.J)) { anim.Play ("Door_Move"); Sound.SetActive (true); Destroy (rope); Destroy (text); } but the anim not run just if i Change void OnTriggerEnter ---> void Updata will run anim )': i want the animation run if i click in button onTrigger how? and thanks you

  • @berkaybakacak
    @berkaybakacak6 жыл бұрын

    Thnaks dude. You are great :)

  • @CubicBrain

    @CubicBrain

    6 жыл бұрын

    Thanks, glad you liked it.

  • @juankacoolx0
    @juankacoolx04 жыл бұрын

    You are the best one

  • @blzy2305
    @blzy23056 жыл бұрын

    How to make this with Left Pressed?

  • @Lord_Piccolo
    @Lord_Piccolo5 жыл бұрын

    Thanks, dude. It is useful.

  • @ChuckyMaster
    @ChuckyMaster5 жыл бұрын

    thank you bro really help ful

  • @vlad-cristiansecatureanu6820
    @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

  • @CubicBrain

    @CubicBrain

    5 жыл бұрын

    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

  • @seddaouiyassine7814
    @seddaouiyassine78145 жыл бұрын

    How if we only want it to play 1 time?

  • @ashoan8965
    @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.

  • @jasielt.2006
    @jasielt.20063 жыл бұрын

    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

  • @CubicBrain

    @CubicBrain

    3 жыл бұрын

    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.

  • @lucutes2936
    @lucutes29364 жыл бұрын

    how do I make them movable with WASD

  • @jimmsdesign-freelancegraph1549
    @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?

  • @joshuamason2227

    @joshuamason2227

    5 жыл бұрын

    attach animator

  • @MrJacky
    @MrJacky5 жыл бұрын

    Omg thank you so much !!!

  • @emilyallen7912
    @emilyallen7912 Жыл бұрын

    It won’t let me add a new state when I right click it just says add behavior

  • @user-bw6br9ym8z
    @user-bw6br9ym8z4 жыл бұрын

    Thank you bro, but if want in ontriggerenter if press keycode E ----> then the animator will play how?

  • @omengaming2019
    @omengaming20193 жыл бұрын

    nice work

  • @rickfuzzy
    @rickfuzzy5 жыл бұрын

    Excellent. Feel like i just leveled up

  • @CubicBrain

    @CubicBrain

    5 жыл бұрын

    Awesome! happy that it helped you!

  • @saipranavkishan7003
    @saipranavkishan70035 жыл бұрын

    One question, does that button initiate all animations on character or only one?

  • @CubicBrain

    @CubicBrain

    5 жыл бұрын

    The script only works on the animations for the specific gameobject. the name of the animation you put into the anim.Play("") is the animation that will playback.

  • @thecomedyduo4001
    @thecomedyduo40013 жыл бұрын

    how to I make the animation keep repeating until i let go of the key?(for walking)

  • @CubicBrain

    @CubicBrain

    3 жыл бұрын

    you might be able to use Input.GetKeyUp to stop the animation

  • @leongrigoruk
    @leongrigoruk6 жыл бұрын

    One of the best

  • @CubicBrain

    @CubicBrain

    6 жыл бұрын

    Thanks. appreciate it.

  • @v3vlog
    @v3vlog5 жыл бұрын

    hey I am not seeing empyt in animation controller

  • @2ndpersonshooter713
    @2ndpersonshooter7136 жыл бұрын

    Thanks, is useful!

  • @icantth1nkofanam40
    @icantth1nkofanam404 жыл бұрын

    mine only works once then it doesnt trigger after that

  • @tonycaterev4100
    @tonycaterev41005 жыл бұрын

    Great Tutorial, thanks!! I've tried to set this script up for a chest, that trigger an animation in the player_controller. But as soon as i try to interact with the chest, the exception: "MissingComponentException: There is no 'Animator' attached to the "Chest" game object, but a script is trying to access it." --- but i dont need the animator of the chest, i need to trigger the animator of my player controller FROM the chest. Did i need to change something in the "anim = GetComponent();" line? public class Lockpicking : MonoBehaviour { public Animator anim; public GameObject lockpick_ui; public Transform ui_spawn_position; public GameObject ui_spawn; public Random rnd = new Random(); public AudioClip lock_success; public AudioClip lock_failure; public AudioClip lock_fatal_failure; //Amount of successful clicks until opening public int lock_level; // Start is called before the first frame update void Start() { anim = GetComponent(); while (lock_level

  • @neillien7989

    @neillien7989

    5 жыл бұрын

    Try attaching it to another game object, also don't forget the collider.

  • @grosbarbu5778
    @grosbarbu57785 жыл бұрын

    It's possible to do the same thing with the E key ? If yes how to do that ?

  • @CubicBrain

    @CubicBrain

    5 жыл бұрын

    Hi Privelol, You should be able to just change the 1 inside the two double quotes to the desired letter.

  • @DAvitArm
    @DAvitArm4 жыл бұрын

    Thanks man you save me :)

  • @ruler1559
    @ruler15594 жыл бұрын

    say if I want to get back to the other animation, how do I do it?

  • @CubicBrain

    @CubicBrain

    4 жыл бұрын

    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.

  • @BojanVasilev
    @BojanVasilev6 жыл бұрын

    hello great tutorial but how can i make when i press again to animation stop and if i press again animation start again

  • @CubicBrain

    @CubicBrain

    6 жыл бұрын

    Hi Bojan, You will have to set up an if statement, that looks for the animation state (which animation is currently playing.)

  • @toddlertech8091
    @toddlertech80917 жыл бұрын

    great tutorial... Thanks

  • @CubicBrain

    @CubicBrain

    7 жыл бұрын

    Thanks man!

  • @anandpattar3106
    @anandpattar31064 жыл бұрын

    How to stop the animation?

  • @Gremlinsmom
    @Gremlinsmom3 жыл бұрын

    This was super helpful! But does anyone know how I can make it stop?

  • @purel2449
    @purel24496 жыл бұрын

    anim.GetComponent().SetTrigger("Round"); //why not ?

  • @hny2585
    @hny25856 жыл бұрын

    Thank you!

  • @silvanacallegari
    @silvanacallegari6 жыл бұрын

    you are AWESOME!

  • @CubicBrain

    @CubicBrain

    6 жыл бұрын

    Thank a lot.