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 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
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.
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.
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.
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.
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.
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 👍
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.
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.
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.
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.
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
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.
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); }
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?
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!
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.
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!
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 ?
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
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.
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
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
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.
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?
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.
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
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!
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
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
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?
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?
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.
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?
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?
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"));
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.
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?
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.
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); }
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!
idle animations dont transition because my value from Input.GetAxisRaw immidately goes back to 0 help!
@@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
I am using Unity 2021 and I guess they changed some things since I never had to add the extra sprite.
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.
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.
For some reason my character is facing down for one frame (when the speed is 1 but horizontal and vertical 0)
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.
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.
thanks!
Many thanks!! I was stucked in that issue
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.
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 👍
Mate, I don't usually comment on UA-cam videos but thank you so much. You're saving my life with this uni assignment...
I've been searching and searching for how to face the right direction when idle and this was exactly what I needed, thank you! :)
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.
DUDE.. you should have kept making videos. You'd 1,000% be at 50k+ by now.
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.
Mate. You're a lifesaver. Thanks a ton!
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.
@@hundredfiresgames2227 Oh really? I had such a hard time finding exactly this answer, so it was quite a relief finding your channel hahah
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.
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! :)
Just what I searched for many hours! Thank you so much
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
Awesome! I was having trouble following other Unity tutorials but yours are very easy to follow thank you very much!
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.
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);
}
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?
Me too. I followed everything in the video but the diagonals are not working.
Hi, did you manage to fix it?
Лучший! Я случайно наткнулся на тебя, и теперь я просто в восторге!!!
Скажи где еще можно поискать тайлмапы?
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!
cheers for the help! works like a charm
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.
Thanks - great tutorial
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!
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 ?
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
Hey man this video was really useful thanks!
Thank you, this really helped me a lot!
Thanks bro! Really helpful!
u saved my life
Really nice ! Thanks man :)
Thanks so much! this helps a lot!
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.
I fixed this by changing the SetFloats to Input.GetAxis instead of Input.GetAxisRaw.
myAnim.SetFloat("lastMoveX", Input.GetAxis("Horizontal"));
@@selfinducedhate2118 brooo thank you so much
@@selfinducedhate2118 thx bro
You save me, thank you very much:')
Do you cover interacting with NPCs/Objects in the tutorial? I mean for example being near them and pushing space.
Awesome thanks
ı wonder why it doesnt work if ı divide horizontal and vertical if controls. only the first if control works and it only face horizonal idles
thank you so much
My player sprite is still facing in the idle down animation after I lift my finger of the keys
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
This has been happening to me too. Did you find a solution?
Me too
Same
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
Did you set all the transitions in the animator?
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.
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?
great video! But for some reason my idle diagonals won't work. they work very occasionally which makes me even more confused.
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.
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
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!
hey man i followed everything but my character is still facing down after movement
ya me too
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
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
THANK YOU!
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?
Thanks mate 💙
Worked for me :)
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?
i have the same thing
same issue, I'm gonna see if I can fix it tm
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.
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?
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?
Thank you (:
having issues with getting == to work. "Operator '==' cannot be applied to operands of type 'string' and 'int'
nvm, solved!
This isnt working for me, can you tell me how to do this in 2021?
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"));
}
I have the same problem. Did you ever find a solution?
14:44 this is what i need to fix face direction
Hmm im still defaulting to IdleDown
me too
So like my dude goes super fast in a couple of directions what would cause this??
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.
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?
nevermind, I was being small brain
my character just slides up and down i have no idea why. any fix??
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.
awesome
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
You rock dude if only the tutorial a bit faster, it would be better
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);
}
THANK YOU SO MUCH THIS REALLY HELPED
Thank you! (for some reason it works 1 time out of 10)
hi i got a problem when i play the with my character it says that lastMoveX and Y does not exit..
Thanks a lot!
Thanks a lot!