Topdown 2D RPG In Unity - 04 Idle & Face Correct Direction

Поділитися
Вставка
  • Опубліковано 29 лис 2024

КОМЕНТАРІ • 108

  • @hundredfiresgames2227
    @hundredfiresgames2227  5 років тому +25

    19:30 So if you look closely at the player while moving left and right, for the first bit of movement it looks like the player is sliding and then going into the animation. This is what i noticed and thought i missed fixing the settings in the transition. i was playing around with it for a little bit trying to figure out how to fix this, and i came up with a very simple solution. when we set the walk animations, we added the sprites in order, and then added the last sprite on the end. the order we used was 0,1,2,3,3. instead, since the 0 animation is the idle position, start with the second and go from there. so now instead of 0,1,2,3,3, change the order to 1,2,3,0,0. this will start the animation with the player walking instead of standing. Hope this helps anyone that this was bugging too!

    • @klaidusx9381
      @klaidusx9381 4 роки тому +2

      idle animations dont transition because my value from Input.GetAxisRaw immidately goes back to 0 help!

    • @rj_1337_
      @rj_1337_ 4 роки тому +1

      ​@@klaidusx9381 I found out that when I clicked on the Player_Idle Blend Tree (not the Base Layer) and looked at my Parameters in the Inspector, I had moveX and moveY instead of lastMoveX and lastMoveY

    • @rtester40
      @rtester40 3 роки тому +2

      I am using Unity 2021 and I guess they changed some things since I never had to add the extra sprite.

    • @NibblyPig
      @NibblyPig Рік тому

      Yup if you enable Loop Pose by clicking on each Anim file in the file browser and checking it in the inspector, you don't need to add a copy of the first frame to the end. Then what the long post above is saying is that the first frame of the walk animation is your guy standing still, which you don't want - you want the first frame to be his leg moving to take a step. So just rearrange the order of frames so the standing still is last.

  • @Witcheridoo
    @Witcheridoo 4 роки тому +17

    This helped a lot! But I recommend for the transitions to use just one variable Speed so you only need one transition from idle to move and from move to idle since Speed is always positive, and you can say less than 1 or greater than 0.

    • @vseodbornik
      @vseodbornik 2 роки тому

      For some reason my character is facing down for one frame (when the speed is 1 but horizontal and vertical 0)

  • @GoMan-21
    @GoMan-21 2 роки тому +1

    Thanks for the video, this is exactly what I was looking for. Every video discussed MOVING in the right direction, but nothing covered IDLING in the right direction.

  • @5secondsago15yearsago4
    @5secondsago15yearsago4 3 роки тому +10

    Thank you so much! I had been searching for this exact tutorial!
    Here's a few things that I learned that may help others a bit, relating to this video:
    1) at 10:18 to 14:15, you don't have to put all those multiple transitions, you can replace it with a "speed" condition.
    So the transitions would be:
    idle to movement = speed greater than 0.1
    movement to idle = speed less than 0.1
    2) I learned this from a simple mistake, pause at 18:22, look closely at that code, if you get a ⚠️warning in your code that says "possible mistaken empty statement", make sure you don't put semicolon at the end of that "if" statement.
    ✔️It should be:
    If (blablabla)
    {
    }
    ❌Instead of:
    If (blablabla);
    {
    }
    I hope that would help! I'm a newbie and thought of sharing some of my mistakes & how I fixed it. If there is anything wrong with the solution I gave, please correct the information.

    • @waxocelot9395
      @waxocelot9395 2 роки тому +2

      thanks!

    • @Gonzilla2009
      @Gonzilla2009 2 роки тому +2

      Many thanks!! I was stucked in that issue

    • @westkossuth
      @westkossuth 9 місяців тому

      Hi, when I do this it force the character to face downward for one frame when transitioning from moving to idle before facing them in the right direction. Any idea why this might be? I have exit time turned off an transition duration set to zero.

  • @Jumph96
    @Jumph96 2 роки тому +3

    Really easy to implement, used part of your method to make it work with my 2D character.. Really appreciate it man, thanks for positing this 3 years ago, hope you're doing well... Stay safe 👍

  • @darrenng9887
    @darrenng9887 3 роки тому +1

    Mate, I don't usually comment on UA-cam videos but thank you so much. You're saving my life with this uni assignment...

  • @w33btrash
    @w33btrash 3 роки тому +3

    I've been searching and searching for how to face the right direction when idle and this was exactly what I needed, thank you! :)

  • @mfitrix
    @mfitrix Рік тому

    Although this was 4 years ago, it still helped get these animations right, very well teaching and explaining, Thank you, though I got issues of all of my direction is inverted 😂and have no idea how to fix it.

  • @ReEvolu
    @ReEvolu 4 роки тому +1

    DUDE.. you should have kept making videos. You'd 1,000% be at 50k+ by now.

  • @charmie20
    @charmie20 2 роки тому

    Thank you so much! Rarely does a Unity tutorial actually solve my exact problem. Didn't have a good solution for facing the correct direction. Perfect. I had different solutions for the other aspects, but that was the missing piece I needed.

  • @plekz_
    @plekz_ 5 років тому +5

    Mate. You're a lifesaver. Thanks a ton!

    • @hundredfiresgames2227
      @hundredfiresgames2227  5 років тому +2

      I'm glad this worked out for you! There are plenty of ways to do this, but i felt this way was the easiest and straight forward way. InScopeStudios has a pretty solid way as well, but it seemed like a bit much.

    • @plekz_
      @plekz_ 5 років тому +3

      ​@@hundredfiresgames2227 Oh really? I had such a hard time finding exactly this answer, so it was quite a relief finding your channel hahah

  • @theginger1187
    @theginger1187 3 роки тому

    im so glad i found this, i was having the same exact problem. walking animation kept playing no matter what i did and he always turned around to face the camera when I let the key go. the closest i got on my own was having him quit looking down but he still wouldnt slip into the idle animation after i let go of the key. all in all, thank you.

  • @Psydexx
    @Psydexx 3 роки тому

    Bro u are actually such a legend oh my god. I was stuck on this for hours and thanks to you I was able to see where I went wrong. Thank you! :)

  • @yoshimalik
    @yoshimalik 3 роки тому

    Just what I searched for many hours! Thank you so much

  • @tdocchillonutube
    @tdocchillonutube 3 роки тому

    These videos are awesome, thank you so much - I'm having a lot of fun! I am surprised at how much it takes just to get one character moving in a 2D game haha

  • @kevinradtke2624
    @kevinradtke2624 4 роки тому

    Awesome! I was having trouble following other Unity tutorials but yours are very easy to follow thank you very much!

  • @SnatchBandegrip
    @SnatchBandegrip Рік тому

    If anyone else has had the problem of your character facing in the wrong direction when hit play -- a sprite's animation will default to the very first animation created for it, and I don't believe there's any way to change it after the fact. So when you start making animations for your character, make sure the first one you create is the one you want it to be by default.

  • @MikhalinYT
    @MikhalinYT 3 роки тому

    Hey! Thanks for the informative vido! It helped me a lot but I changed a bit the code to use it in my game, so that the Player turns where the mouse is. Also I put only 1 condition on the animation trigger.
    Update:
    Vector3 mousePossition = mainCamera.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Horizontal") == -1 || Input.GetAxisRaw("Vertical") == -1)
    {
    PlayerMovingAnimation(mousePossition);
    }
    else if (Input.GetAxisRaw("Horizontal") == 0 || Input.GetAxisRaw("Vertical") == 0)
    {
    PlayerIdleAnimation(mousePossition);
    }
    And the 2 below, in 2 nice refactored methods.
    private void PlayerIdleAnimation(Vector3 mousePossition)
    {
    animator.SetBool("IdleWatch", false);
    animator.SetFloat("WatchX", mousePossition.x);
    animator.SetFloat("WatchY", mousePossition.y);
    }
    private void PlayerMovingAnimation(Vector3 mousePossition)
    {
    animator.SetBool("IdleWatch", true);
    animator.SetFloat("Horizontal", mousePossition.x);
    animator.SetFloat("Vertical", mousePossition.y);
    }

  • @MrSeriousClown
    @MrSeriousClown 3 роки тому +3

    Great video! Two things, I think your final if condition can simply be: `Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0`. Also, my character is having trouble stopping at the diagonal position. Any idea why?

    • @JoJo-tx2qn
      @JoJo-tx2qn 3 роки тому

      Me too. I followed everything in the video but the diagonals are not working.

    • @LIB0RG
      @LIB0RG Рік тому

      Hi, did you manage to fix it?

  • @romanmishenin
    @romanmishenin 3 роки тому +1

    Лучший! Я случайно наткнулся на тебя, и теперь я просто в восторге!!!
    Скажи где еще можно поискать тайлмапы?

  • @LaserGadgets
    @LaserGadgets 7 місяців тому

    I was stuck in idle and there was no way out, parameters said 1, conditions set to if>1 then walk but nope.
    Forget the horX vertY stuff. Just say condition IF SPEED >0.1 ! One line, and my player is walking and idling!

  • @AlexJasper123
    @AlexJasper123 3 роки тому

    cheers for the help! works like a charm

  • @procex1514
    @procex1514 3 роки тому

    I leave a little fix here so if you struggle like I did you can still carry on.
    Sometimes the sprite is not changing into the correct idle state cause it's waiting for 1 or -1.
    That means that if you move just 0.01 or 0.01 (in any axis), it doesnt get to change its idle position, cause each idle state is waiting for a whole 1 or -1.
    What I did was simply change that last if statement so "Horizontal" was == 0.1 and -0.1 (instead of 1 and -1 respectively and same goes for Vertical.
    Now I you change my idle your idle states even moving just a single bit :D
    Hope it helps somebody.

  • @damegeveloper8648
    @damegeveloper8648 2 роки тому

    Thanks - great tutorial

  • @SpealingMisteak
    @SpealingMisteak 4 роки тому

    Loving these videos,, my first time using unity and c#, great helpful and to the point tutorials!
    For the idle animations i Imade a new variable 'speed'.
    And added the following line in the script :
    myAnim.SetFloat("speed", myRB.velocity.magnitude);
    And within the player_move to player_idle transition i just have one condition where if speed is less than 0.1 and appears to be working fine.
    Can use this instead if any of you are having issues with the other ways.
    Thanks!

  • @mck3d605
    @mck3d605 4 роки тому +4

    Hello, i have a problem where everything is working, but sometimes when i go up right or up left, down right, down left he decides to idle either up or left but not left up, but somtimes it works, its like flipping a coin. Can u help me fix that ?

  • @berrenstone4977
    @berrenstone4977 4 роки тому +1

    Yea there's definitely better ways to do the animation here, but it will work as he's just shown. Great set of videos though. Like Ryan Cabell said you can just use speed instead of both x and y. Create a movementSpeed float and throw this in update function " movementSpeed = myRB.velocity.magnitude; ". Then wherever your throwing your animator stuff, I use late update which is just updated but it runs after everything else has been calculated, throw this " animator.SetFloat("Speed", movementSpeed); ". Create that Speed float in your animator and then you can just say if "Speed" is greater than 0.001 then move to walk animation

  • @shubpokemon2000
    @shubpokemon2000 3 роки тому

    Hey man this video was really useful thanks!

  • @lauravergara4137
    @lauravergara4137 3 роки тому

    Thank you, this really helped me a lot!

  • @Megahertzs
    @Megahertzs 3 роки тому

    Thanks bro! Really helpful!

  • @aytekinnnnn
    @aytekinnnnn Рік тому

    u saved my life

  • @durrium
    @durrium 3 роки тому

    Really nice ! Thanks man :)

  • @seighart90
    @seighart90 3 роки тому

    Thanks so much! this helps a lot!

  • @nathan12581
    @nathan12581 4 роки тому +6

    How did you get the diagonal idle animations to play correctly, as in my game I've used the same system, just that the player has to let go of both of the keys (for example W and D) at the exact same time to get it to play the correct diagonal aniamtion.

    • @selfinducedhate2118
      @selfinducedhate2118 3 роки тому +4

      I fixed this by changing the SetFloats to Input.GetAxis instead of Input.GetAxisRaw.
      myAnim.SetFloat("lastMoveX", Input.GetAxis("Horizontal"));

    • @dudugeorge653
      @dudugeorge653 Рік тому

      @@selfinducedhate2118 brooo thank you so much

    • @ViejoSabrosod
      @ViejoSabrosod Рік тому

      @@selfinducedhate2118 thx bro

  • @Moxis12883
    @Moxis12883 3 роки тому

    You save me, thank you very much:')

  • @QFGEE
    @QFGEE 3 роки тому

    Do you cover interacting with NPCs/Objects in the tutorial? I mean for example being near them and pushing space.

  • @neozoid7009
    @neozoid7009 Рік тому

    Awesome thanks

  • @fIb6914
    @fIb6914 Рік тому

    ı wonder why it doesnt work if ı divide horizontal and vertical if controls. only the first if control works and it only face horizonal idles

  • @phunguyen-lp6ii
    @phunguyen-lp6ii Рік тому

    thank you so much

  • @freddyyy29
    @freddyyy29 4 роки тому

    My player sprite is still facing in the idle down animation after I lift my finger of the keys

  • @anthonycidmendoza1535
    @anthonycidmendoza1535 4 роки тому +2

    Sup man, I've got the same problem with other guy that said that it kept going back to the downwards idle bit, I have the same problem but I'd like to point out that when letting go of the movement key, "lastMovementX and y" keep going back to zero instead of 1

    • @alexshltn
      @alexshltn 4 роки тому +1

      This has been happening to me too. Did you find a solution?

    • @etgan1844
      @etgan1844 4 роки тому +1

      Me too

    • @rj_1337_
      @rj_1337_ 4 роки тому

      Same

  • @ep1c183
    @ep1c183 5 років тому +3

    hello I follow yoour steps and naming of all your codes but my character keeps idling in the same idle over and over again it didn't keep his idle when he moves in any direction please help

    • @hundredfiresgames2227
      @hundredfiresgames2227  5 років тому

      Did you set all the transitions in the animator?

    • @hundredfiresgames2227
      @hundredfiresgames2227  5 років тому +2

      ok, new solution if you did not figure it out already...or give up on it. been going back through this series, and i had the same issue as you did and i could not figure out what was going on for about 30 min. turns out, i forgot to set the animation (the single sprite) for the WalkUp, Down, Left, and Right animations, and only set it for the diagonal directions. after setting these, it worked fine. Hope that helped, and i hope you are able to continue the series.

  • @Steve-gi2yj
    @Steve-gi2yj 3 роки тому

    thank you for this tutorial, however I am stuck, its similar to your code, but not sure how I can incorporate a joystick if its a mobile type instead of pc or console, any thoughts?

  • @tommybasile2931
    @tommybasile2931 Рік тому

    great video! But for some reason my idle diagonals won't work. they work very occasionally which makes me even more confused.

    • @SnatchBandegrip
      @SnatchBandegrip Рік тому

      I have found they work if you let go of both directional buttons at EXACTLY the same time, which comes down to hundredths of a second. It will otherwise default to either one.

    • @cigslit
      @cigslit Рік тому

      I just read that it worked for someone changing the SetFloat of ''lastMoveX'' and ''lastMoveY'' to Input.GetAxis instead of Input.GetAxisRaw ...
      I don't know if it actually works but you can try

  • @zaneshober8838
    @zaneshober8838 2 роки тому

    Hi! For some reason my character does not always want to stay facing the last direction when moving diagonally. If I do not lift both keys when coming to a stop diagonally then the character sometimes defaults to the left, right, up, or down idle pose instead of the diagonal one. Any thought on how to fix this? Thanks!

  • @EliteGamer689
    @EliteGamer689 3 роки тому +1

    hey man i followed everything but my character is still facing down after movement

    • @oximas
      @oximas 3 роки тому

      ya me too

  • @DanXD64
    @DanXD64 4 роки тому +1

    I realize that my idle animations dont transition because my value from Input.GetAxisRaw immidately goes back to 0, how do I fix this? if anyone could help that would be amazing, tyia

  • @mackabuze350
    @mackabuze350 3 роки тому

    I need help I have an issue where my IdleUp, IdleLeft and IdleRight start bugging and agressively switching from left to down in game when I stop moving, only IdleDown works normally

  • @EveryMonthLikes
    @EveryMonthLikes 3 роки тому

    THANK YOU!

  • @cynthiasheard9252
    @cynthiasheard9252 4 роки тому

    I had the issue where if I tapped my directional keys, it would still default to idle down, but if I held them for longer than that the ending idle positions worked. I will fiddle around to see if I can find out why, but I wanted to know if anyone else was having a similar issue?

  • @losfouad2014
    @losfouad2014 4 роки тому

    Thanks mate 💙

  • @kingmumblez6577
    @kingmumblez6577 5 років тому +1

    Worked for me :)

  • @mkdemo9348
    @mkdemo9348 4 роки тому +2

    Hey, thanks for the tutorial! I can't seem to get the diagonal idle states to play. The lastMovementY always go back to 0 and either the left/right/top/down idle animation is played rather than the up-left/up-right etc. counter parts. Any ideas?

    • @barryblokhut886
      @barryblokhut886 3 роки тому

      i have the same thing

    • @yaboishadowninja
      @yaboishadowninja 2 роки тому

      same issue, I'm gonna see if I can fix it tm

    • @yaboishadowninja
      @yaboishadowninja 2 роки тому

      hey so I looked it over, and I think the reason they aren't playing is because when you release the buttons you're pressing, you're only releasing one button. so if youre going upleft, you have to release the up and left buttons at the same time for the upleft idle animation to play. if you release up prematurely, you'll get the left animation, and vice versa.

  • @prodromossaklamakis2667
    @prodromossaklamakis2667 3 роки тому

    Great Series! But I've got a problem in this part. For the idle animation, when I try to move with my controller, it works just fine, but the idle position, most of the times, is not correct. It goes into a random one. I think it's because of the joystick movement when it resets its position. Do you have any way of fixing this?

  • @rtester40
    @rtester40 3 роки тому

    How would one transition to the roll animation from idle and walk? do we make 2 separate blend nodes? 1 connected to the walk and 1 connected to the idle, Or is it better to have it return back to both other nodes?

  • @unknownchannel65
    @unknownchannel65 3 роки тому +1

    Thank you (:

  • @bilcifer666
    @bilcifer666 4 роки тому

    having issues with getting == to work. "Operator '==' cannot be applied to operands of type 'string' and 'int'

  • @chickenmaster3658
    @chickenmaster3658 3 роки тому

    This isnt working for me, can you tell me how to do this in 2021?

  • @Polygon_Parrot
    @Polygon_Parrot 2 роки тому

    When I move Up, Down, Left, and Right, the run animation goes to the idle animation just fine after movement. However, when I go NE, NW, SE, SW the idle animation with not stay in the proper direction of the angle, but decide to choose either North, East, or NE (North East) when I am trying to keep it in the North-East direction.
    My understanding is that the lastMoveX & lastMoveY will activate a 1, 0, -1 in the direction that is associated with the movement, but won't hold onto the required 1, 0, -1 in order to keep the proper angle direction of the character specifically with the angled directions.
    What should I do to fix this?
    if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1 || Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
    {
    myAnim.SetFloat("lastMoveX", Input.GetAxisRaw("Horizontal"));
    myAnim.SetFloat("lastMoveY", Input.GetAxisRaw("Vertical"));

    }

    • @zaneshober8838
      @zaneshober8838 2 роки тому

      I have the same problem. Did you ever find a solution?

  • @nhanvuong8481
    @nhanvuong8481 4 роки тому +1

    14:44 this is what i need to fix face direction

  • @RavenCroftLoft
    @RavenCroftLoft 4 роки тому +5

    Hmm im still defaulting to IdleDown

  • @ir3dyplayz78
    @ir3dyplayz78 3 роки тому

    So like my dude goes super fast in a couple of directions what would cause this??

    • @shubpokemon2000
      @shubpokemon2000 3 роки тому +1

      He goes fast in the diagonal directions because the vector responsible for your player movement is not "normalized". This is a super common bug where when you press two keys your player is moving at 40% increased speed. To fix it just find the name of your vector2D variable and do something like this (my vecotr2D variable is called 'movement'):
      movement = movement.normalized;
      you need to add that code right after you initialize the horizontal and vertical getaxis methods. I think this should fix your problem, but I am also kinda new to this.

  • @kangrroo5596
    @kangrroo5596 4 роки тому

    I have a problem whenever I stop moving the character facing up, right or left the idle animation bugs out, constantly swapping between the idle animation it is meant to be, and the down idle about 50 times a second. The idle works fine when I move down, just not for left, right and up. Anyone have any help?

    • @kangrroo5596
      @kangrroo5596 4 роки тому

      nevermind, I was being small brain

  • @bartoszhd9838
    @bartoszhd9838 5 років тому

    my character just slides up and down i have no idea why. any fix??

    • @hundredfiresgames2227
      @hundredfiresgames2227  5 років тому

      There could be a couple of reasons for this. make sure everything is set up correctly in the animator, also double check that the spelling of the parameters, and make sure that it is the same in the coding. between the " " should be EXACTLY the same, caps and all.

  • @DanXD64
    @DanXD64 4 роки тому

    awesome

    • @DanXD64
      @DanXD64 4 роки тому

      I have a slight problem tho, after adding the new code in the script, it still doesn't face correctly and still goes back to the default idle sprite

  • @bodybody5609
    @bodybody5609 4 роки тому

    You rock dude if only the tutorial a bit faster, it would be better

  • @c0pyrght774
    @c0pyrght774 4 роки тому +1

    Hey so i see some people having some problems with getting it to stay in the right direction. Ive watched this video and a few others and came up with this script for it to stay in place. Hope this helps anyone :p Also a movement script in there aswell heh
    private Rigidbody2D rb;
    private Animator myAnim;
    private Vector2 moveVelocity;
    public float speed;
    Vector2 movement;
    void Start()
    {
    rb = GetComponent();
    myAnim = GetComponent();
    }
    void Update()
    {
    movement.x = Input.GetAxisRaw("Horizontal");
    movement.y = Input.GetAxisRaw("Vertical");
    myAnim.SetFloat("moveX", movement.x);
    myAnim.SetFloat("moveY", movement.y);
    if(Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1 || Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1)
    {
    myAnim.SetFloat("lastMoveX", movement.x);
    myAnim.SetFloat("lastMoveY", movement.y);
    }
    }
    void FixedUpdate()
    {
    rb.MovePosition(rb.position + movement * speed * Time.fixedDeltaTime);
    }

    • @Fling569
      @Fling569 Рік тому

      THANK YOU SO MUCH THIS REALLY HELPED

    • @LIB0RG
      @LIB0RG Рік тому

      Thank you! (for some reason it works 1 time out of 10)

  • @bubiti69
    @bubiti69 3 роки тому

    hi i got a problem when i play the with my character it says that lastMoveX and Y does not exit..

  • @machebermejo4857
    @machebermejo4857 3 роки тому

    Thanks a lot!

  • @VividPastels
    @VividPastels Рік тому

    Thanks a lot!