Hi ! This is a great vid thanks ! No offense, but your code looks very complicated and can be refactored a lot using Vector2().normalized method. Void FixedUpdated() { rb.velocity = new Vector2(inputHorizontal, inputVertical).normalized * walkSpeed ; } With this, no need to set up a speed limiter since is Always returns a vector with magnitude of 1. Meaning Diagonal movement get 1 * walkspeed. Plus, no need to check IF key is pressed since Input.GetAxisRaw will be 0 to 1 so if no key is pressed nothing in happening anyway right? Good day to you good sir :)
@Dani_Krossing2 жыл бұрын
You are completely right. 🙂 Looking back I would have done it that way too. I’ll pin your comment for others to see. 😊
@jehriko75252 жыл бұрын
I just want to say I've been struggling with this for a while and this video has helped me immensely, thanks for making it!
@twist222 жыл бұрын
Thanks so much for this! I've been agonizing over using the animator visual representation with transitions and all that jazz and my head's been spinning over a bug I couldn't figure out. I reworked in code and the whole thing feels way simpler now. Thank you! :D
@Hamlet26152 жыл бұрын
This is so recent. You are the new generation brackeys my guy. love this!
@clark7831 Жыл бұрын
you are literally the goat thank you!
@premiumdrawback Жыл бұрын
This helped so much! Thank you!
@serviceallies Жыл бұрын
For any fellow nerds who like to make sure the math is precise: to make sure the diagonal movement speed perfectly matches the regular speed, the correct equation is diagonal movement speed times 0.707106. So even .7 is pretty darn close
@JustinDunn_IT2 жыл бұрын
Bro, you are completely underrated. The laymen explanations as you go through for why you do something 100% is how I learn. Sure, maybe code can be refactored like @Don't Blink said, buuut for someone like me (with slight coding history) just trying to get off the ground (and understands the first way you do something is not always the optimal) its a godsend for them. So thanks.
@sreenjoymodak66363 жыл бұрын
hey! I love your tutorials they are the best tutorials one could ask for! I've already completed the HTML/CSS and the javascript courses. Keep it up!
@xavierromeo7781 Жыл бұрын
Hi ! Thank you really much, you helped me a lot. I reworked your code a little bit to use different idle animations depending on the last direction. I have a direction integer to store the last direction And I use switches and cases instead of a bunch of if-statements.
@jasminemerritt1831 Жыл бұрын
Thank you for making this!
@ph0xnix3732 жыл бұрын
I really like the way you explain what each line is actually doing. Makes things make a lot more sense. Have you done anything with the new unity input methods? Feel like they streamline the movement process a lot in some other tutorials I found. Just think you do a much better job of explaining what is actually happening and what various codes do.
@James_GamesYT Жыл бұрын
WTH THIS IS SO COOL
@stealthyshiroean3 жыл бұрын
Huh, that is different. I originally was going to skip over this video because I already had a working top-down movement controller for my player. However, I wanted to see how you did it and thought it would be good for the learning process. Didn't expect a state machine! I think a lot of tutorials do talk about using the animator and creating blend trees then manipulating those through the code which is pretty much what I did. Also, you did bring up a very important point that I forgot about. My character moves too quickly when walking diagonally lol. I'll need to fix that. Thanks for the great tutorial and the different approach!
@Mr.Zita13 жыл бұрын
Thank you for the courses Mr Dani 🤓👍
@HullaBiloo2 жыл бұрын
Hi I was just wondering if there was any way to change it so instead of reverting the player back to the front idle animation you could make it so that the idle animation changes depending on the last input made by the player? Like an Idle_Left animation playing after moving left instead of the player going back to the default and looking down afterwards. Great tutorial by the way!
@NeosRS2 жыл бұрын
I have this same question actually, would love to know how to update it to do this.
@Jonny-op3wr2 жыл бұрын
Were you able to figure it out? If not he posted a simple solution in another persons comment. It was to make basically idle states for each direction they face and basically do check statements.
@troyna77 Жыл бұрын
you would need a variable to "remember" the last state before changing into the new. kindof like a temp/holding variable. then as you exit the loop or code block, perform an if (condition- ex. _tempState == _theStateYouWantToRevertBackTo) THEN {current variable = temp/holding variable}; else (continue without changing animation state); im sure there are a few other more sophisicated ways to handle this.
@onigiri_sakaro Жыл бұрын
if anyone is searching for this, i achieved it by saving a "currentDirection" string when moving and adding 4 Idle States for each direction. i'll post the full code below. hope it helps! :) public class PlayerMovementAnimate : MonoBehaviour { public float moveSpeed; float inputHorizontal; float inputVertical; private Rigidbody2D rb; private Vector2 moveDirection; Animator animator; string currentState; string Player_Idle = "Player_Idle"; string Player_IdleUp = "Player_IdleUp"; string Player_IdleDown = "Player_IdleDown"; string Player_IdleLeft = "Player_IdleLeft"; string Player_IdleRight = "Player_IdleRight"; string Player_WalkUp = "Player_WalkUp"; string Player_WalkDown = "Player_WalkDown"; string Player_WalkLeft = "Player_WalkLeft"; string Player_WalkRight = "Player_WalkRight"; string currentDirection; void Start() { rb = gameObject.GetComponent(); animator = gameObject.GetComponent(); } public void Update() { ProcessInputs(); } public void FixedUpdate() { Move(); } public void ProcessInputs() { inputHorizontal = Input.GetAxisRaw("Horizontal"); inputVertical = Input.GetAxisRaw("Vertical"); moveDirection = new Vector2(inputHorizontal, inputVertical).normalized; } public void Move() { rb.velocity = new Vector2(moveDirection.x * moveSpeed, moveDirection.y * moveSpeed); if (inputHorizontal == 0 && inputVertical == 0) { if (currentDirection == "Right") ChangeAnimationState(Player_IdleRight); if (currentDirection == "Left") ChangeAnimationState(Player_IdleLeft); if (currentDirection == "Up") ChangeAnimationState(Player_IdleUp); if (currentDirection == "Down") ChangeAnimationState(Player_IdleDown); } else if (inputHorizontal > 0) { ChangeAnimationState(Player_WalkRight); currentDirection = "Right"; } else if (inputHorizontal < 0) { ChangeAnimationState(Player_WalkLeft); currentDirection = "Left"; } else if (inputVertical < 0) { ChangeAnimationState(Player_WalkDown); currentDirection = "Down"; } else if (inputVertical > 0) { ChangeAnimationState(Player_WalkUp); currentDirection = "Up"; } } void ChangeAnimationState(string newState) { if (currentState == newState) { return; } animator.Play(newState); currentState = newState; } }
@cooliogamerz3523 Жыл бұрын
@@onigiri_sakaro Thank you so much, this is literally the first time ive replied to a youtube comment. Im pretty new and was struggling to figure out how to code dynamic idle animations using a state machine and this comment saved me. Great comprehensive code!
@erentst2 жыл бұрын
thanks for the tutorial!! have a great day sir
@rodrigochan39612 жыл бұрын
muchas gracias master sigue asi muy buenos tus tutos saludos desde mexico
@WarmFuzzlyBear2 жыл бұрын
Thank you! Subbed
@dashracer82 Жыл бұрын
Could you show me how, with this code, I could use the flipped variation of the WalkRight animation as a WalkLeft? My tileset only has right animations for walking and attacking, and death. or would it be easier to just add left animations to my tileset?
@UnstableStrafe2 жыл бұрын
For fixing the diagonal movements, I'm curious why you used a float instead of normalising the vector?
@Dani_Krossing2 жыл бұрын
Normalize is perfectly fine too, and a simpler solution if you just need equal speed in all directions. 🙂 The main difference is that the way I did it allow for more customization.
@WarmFuzzlyBear2 жыл бұрын
The Animator tree was supper hard this was easy to follow
@scottisitt2 жыл бұрын
That’s really cool that we can control the animation state with code instead of the blend tree! Thanks for showing us that! Question…if we do it through code, do we still need the animation states to be in Animator (as in 23:48)? EDIT: I'm guessing the answer is yes, since that's the player controller...
@Dani_Krossing2 жыл бұрын
You are correct, we do still need them. 🙂
@scottisitt2 жыл бұрын
@@Dani_Krossing Thought so :) Thanks, again!
@atharvalangote82583 жыл бұрын
What if we want to keep the character looking in the direction after we stop pressing arrow keys?
@gralegath2 жыл бұрын
Another set of sprites and then inside the state machine you'd need another set of states, somthing like PLAYER_IDLE_LEFT/RIGHT And probably another variable that stores your previous movement, just like newstate updates current state, only it dosent update with the new one.
@vilanstrikegaming51143 жыл бұрын
Cool sprites
@Dani_Krossing3 жыл бұрын
You are too kind hahaha 😂
@vilanstrikegaming51143 жыл бұрын
@@Dani_Krossing yeah
@vipex55662 жыл бұрын
thank you so much you are the bestt
@Pimple3692 жыл бұрын
What software are you using when you write your code? It just opens notepad for me.
@Dani_Krossing2 жыл бұрын
When you install a Unity version, it prompts a window where you can choose to install Visual Studio as well. This is the editor that most use with Unity. 🙂 However I recommend downloading Visual Studio directly off of Microsoft's website, in order to get the latest version, since Unity prompts you to install the older 2019 version. I have a "how to install Unity" video on my channel, that shows how to set up Visual Studio correctly with Unity. I recommend watching that afterwards.
@Pimple3692 жыл бұрын
Ah, alright, thank you!
@charlieelane20472 жыл бұрын
How would I set up collisions from here so that my player stops walking through trees. I've got it set up on my tile map and have the box collider 2D on my player but no idea how to code it
@kimbosan1 Жыл бұрын
Hello Elon Musk, thanks for great tutorial.
@wstkevin41612 жыл бұрын
At 21:04 you mean diagonally again right? 😅
@SirAntonic2 жыл бұрын
Quick question (I Hope) if i were to have the ending animation frame facing a different direction then default idle (aka an idle animation for left,right,up, and down) how would I go about having the code see that, The code currently if I'm walking and stop walking to the left the character snaps to facing forward instead of staying to the left.
@Dani_Krossing2 жыл бұрын
You need to create idle animation sprites for all the directions, and just activate those depending on the direction you last moved
@gasmaskguy1232 жыл бұрын
@@Dani_Krossingcould you please tell how do you do that? I know zero C# 🥲 (how to activate them on the direction last moved)
@corbinbujnowski4016 Жыл бұрын
I'm a newbie so take my advice with a grain of salt, but what I did was play the walking animation when Input.GetKeyDown then play the idle animation when Input.GetKeyUp. However, the animation is not fluid at all so I'm looking for a better way. What I would suggest doing if you create an animation state dependent on the virtual axis (like in the video), is to play the idle animation when the rigidbody's velocity is 0. I haven't tried this yet so I don't know if it works. I hope this helps a little bit if you still have this problem.
@sakshampathak48702 жыл бұрын
What version of unity are you using?
@Dani_Krossing2 жыл бұрын
2020.3.25f1
@williamdahlstedt9532 Жыл бұрын
can someone help me, my character keeps drifting up and left, if I press right it moves right and up and if I press Down it moves left and down, I've tried multiple tutorials and the same problem
@zoxemo38382 жыл бұрын
give me more man ;D
@Tae_Grixis2 жыл бұрын
I've seen three different videos and they all did movement in three different ways. Does it change over time or are there really just many ways to do the same thing?
@Dani_Krossing2 жыл бұрын
There are many ways to accomplish the same thing. Although in this video I would use the method I pinned a comment about, where a person suggested normalizing the vectors. 🙂
@Tae_Grixis2 жыл бұрын
@@Dani_Krossing Thanks for the information.
@roythedroyd16942 жыл бұрын
so I'm using visual studios 2019 and rigid body 2d will not show up when I type it in. if anyone has a way to fix this it would be greatly appreciated.
@fishstack2 жыл бұрын
What can i do if i get a CS0103 error? "Assets\Player\Scripts\PlayerController.cs(56,38): error CS0103: The name 'PLAYER_WALK_DOWN' does not exist in the current context"
@CoryPelizzari Жыл бұрын
Wait, why not just use 'normalized' for the horizontal and vertical vectors? I'm confused.
@Dani_Krossing Жыл бұрын
Pinned comment 🙂
@bendugamr_the_blobfish Жыл бұрын
I copied everything exactly and for some reason it just doesn’t work. I click the buttons and it just sits there
@katiesanders37379 ай бұрын
i've been trying different movement tutorials and although each one is different, my character always moves down nonstop when i run the program. is there a reason for this that i'm missing?
@Dani_Krossing9 ай бұрын
If your character starts running as soon as you start the application, then it’s most likely the input logic that has a typo. If your code looks the same as mine, it should only run if you press a movement key. 🙂
@ItsAxoReal2 жыл бұрын
using System.Collections; using System.Collections.Generic; using UnityEngine; public class JoystickPlayerExample : MonoBehaviour { public Joystick joystick; private Vector3 input; public float Speed = 5f; private void Update() { input = new Vector3(joystick.Horizontal, joystick.Vertical); Vector3 velocity = input.normalized * Speed; transform.position += velocity * Time.deltaTime; } }
@FrenchFried_ckd2 жыл бұрын
you chad
@blvshh Жыл бұрын
The moment I started watching this I realized that you didn't explain how to import the player sprite and that's my main problem, Great tutorial but now I have to watch a whole separate video and it's a little bit annoying
@Dani_Krossing Жыл бұрын
Neither the thumb or title on this video has anything to do with importing sprites though. 🙂 I do have a video that specifically goes into how to moody sprites in my Unity playlist.
@francoisdebruin62932 жыл бұрын
Quick question i copied everything and everything works perfect only problem i have is it does not return to idle animation after buttons are stopped pressed any clues where to look? when i star the game the idle animation is just fine
@Dani_Krossing2 жыл бұрын
I can with a 100% guarantee you that it is because of a typo. 🙂 Paste your code here and I'll take a look.
@francoisdebruin62932 жыл бұрын
@@Dani_Krossing using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { //Components Rigidbody2D rb; //Player float walkSpeed = 7f; // NOT IN USE float speedLimiter = 10f; float inputHorizontal; float inputVertical; //Animations and states Animator animator; string currentState; const string PLAYER_IDLE = "Player_Idle"; const string PLAYER_WALK_LEFT = "Player_Walk_Left"; const string PLAYER_WALK_RIGHT = "Player_Walk_Right"; const string PLAYER_WALK_UP = "Player_Walk_Up"; const string PLAYER_WALK_DOWN = "Player_Walk_Down"; // Start is called before the first frame update void Start() { rb = gameObject.GetComponent(); animator = gameObject.GetComponent(); } // Update is called once per frame void Update() { inputHorizontal = Input.GetAxisRaw("Horizontal"); inputVertical = Input.GetAxisRaw("Vertical"); if (inputHorizontal > 0) { ChangeAnimationState(PLAYER_WALK_RIGHT); } else if (inputHorizontal < 0) { ChangeAnimationState(PLAYER_WALK_LEFT); } else if (inputVertical > 0) { ChangeAnimationState(PLAYER_WALK_UP); } else if (inputVertical < 0) { ChangeAnimationState(PLAYER_WALK_DOWN); } } void FixedUpdate() { rb.velocity = new Vector2(inputHorizontal, inputVertical).normalized * walkSpeed; ChangeAnimationState(PLAYER_IDLE); } // Animation State Changer void ChangeAnimationState(string newState) { // Stop animation from interrupting itself if (currentState == newState) return; //Play new animation animator.Play(newState); //Update Current state currentState = newState; } }
@francoisdebruin62932 жыл бұрын
thanx brother
@Dani_Krossing2 жыл бұрын
Your ChangeAnimationState(Player_Idle); needs to go inside a “else” statement, at the end of your “else if” chain inside Update() 🙂 I think that’s how I did it in the video too.. Right?
@francoisdebruin62932 жыл бұрын
@@Dani_Krossing bro thanx allot
@cardikAssss Жыл бұрын
Character over accelerates on vertical and there-s a weird delay following this tutorial and the pinned comments
@valtris9942 жыл бұрын
bro looks like elon musk
@leonschaub74142 жыл бұрын
I copied the Code perfectly yet it still wont work. I can see the inputs getting registered but there is no applied velocity.
@Dani_Krossing2 жыл бұрын
99% of the time when it's not working, it's because people made a typo or error while following the tutorial. Even if they said they "copied the code perfectly" 🙂 If you did everything like in the video, it should work.
@leonschaub74142 жыл бұрын
@@Dani_Krossing ill just restart from scratch and follow everything cause i already did some things that might have messed it up. I only recently started working with unity and C#.
@Dani_Krossing2 жыл бұрын
@@leonschaub7414 Learning how to trouble shoot is a skill that you will get good usage off, instead of starting over. 🙂 You will encounter errors and mistakes constantly while developing games. I'm guessing you didn't get any error messages since you don't know what is causing it... Did you make sure to add the script to the player? If you did add the script, then it is either the RigidBody not getting grabbed using 'GetComponent()' or it is a typo you made either in the 'if statement' where you check for movement, or when adding 'AddForce'.
@Mlkym1lk2 жыл бұрын
I followed the code but my sprite is not moving...I tried to code it again but it still doesn't move. How do I fix it?
@Dani_Krossing2 жыл бұрын
Either you made a typo in your code (twice), you made a mistake in the Unity editor, or you are doing something different and not following the tutorial a 100% (for example implementing it into your own game). In 99% of the cases, that is what is causing the issue. Nothing in this tutorial is wrong or outdated. 🙂
@Mlkym1lk2 жыл бұрын
@@Dani_Krossing I see. I will try it again and recheck if there're any errors. Thank you so much!
@Basulisk2 жыл бұрын
I’m confused and this is probably the wrong place to ask this but unity says I can’t start a statement with “else” (Idk how to code at all)
@Basulisk2 жыл бұрын
😓
@Dani_Krossing2 жыл бұрын
I could explain to you why you get the error, but I don't think it would do you much good in the long run. 😕 Like you mentioned, you haven't learned any C# yet, which means you will struggle a lot with most code you write, because you jumped ahead in your learning a bit too fast. So here is what I recommend you do... Unity require than you know how to code at least a little bit, since coding goes hand in hand with game dev. 🙌 I know "programming" can sound scary if you are new, and I know that you are eager to get started on making a lot of fun games in Unity 🙂 ... but I can't stress enough HOW MUCH you would benefit from learning a bit of C#, before you start making your first game in Unity. I promise that you will be able to learn it fairly easy. 🙂 In this playlist I have here, you will learn how to do all the essentials when it comes to C# programming in Unity. It is explained specifically with COMPLETE beginners in mind, so it is a good place to start. It will also teach you why you are getting that error. 😉 kzbin.info/www/bejne/rqiZdYVtqtqBsJI&ab_channel=DaniKrossing
@Dani_Krossing2 жыл бұрын
I should mention that the playlist is still on-going, and I am currently adding more videos to it. 🙂 If you want to skip ahead from that playlist I linked and try a more "complete playlist", then I have a "pure C# playlist" which isn't specifically related to Unity, but it WILL be something you use in Unity too. 🙂 kzbin.info/www/bejne/fnOUkoOPhbV9aJY&ab_channel=DaniKrossing
@Chasealex11452 жыл бұрын
i keep geting the same error but unity does not tell me what it is
@Dani_Krossing2 жыл бұрын
I can't help either unless I get the error hehe. Or at least a description of what is wrong.
@Chasealex11452 жыл бұрын
@@Dani_Krossing It tells me the imput does not go were it is in the context of the code
@kupsoboy5702 жыл бұрын
it never works when i do it:('
@cardchronichlestcg9252 жыл бұрын
kinda look like elon of the musk variety
@kaigoprod32382 жыл бұрын
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. ty for this usefull tutorial
@Dani_Krossing2 жыл бұрын
You skipped a step in the video 🙂 Make sure you add the animator component in the Animation window.
@tuncyturan2 жыл бұрын
Add Animator component to Player object and drag your animation controller to object of animation controller propertiy
@geometrydash21182 жыл бұрын
why am i geting no movement ???????????????
@Dani_Krossing2 жыл бұрын
Most likely a typo or mistake 🙂
@IberianInteractive Жыл бұрын
nah bruh... i get this mistake... Gravedad Código Descripción Proyecto Archivo Línea Estado suprimido Error CS0104 'Input' es una referencia ambigua entre 'UnityEngine.Input' y 'UnityEngine.Windows.Input' Assembly-CSharp
@Dani_Krossing Жыл бұрын
Brah dude... Gotta send it in English if I am to help. 😉
@homerocapuano7902 жыл бұрын
Mine face 22 errors
@cartermcknight40542 жыл бұрын
I keep getting complier issues
@Dani_Krossing2 жыл бұрын
What compiler error message are you getting? It should tell you in the Unity console. Always share that. 🙂
@cartermcknight40542 жыл бұрын
@@Dani_Krossing thats the funny bit theres nothing in the console
@scottisitt2 жыл бұрын
Isn't Input.GetAxisRaw() using the old input system? Is there a way to modify this code using the new input system? I mean, I'll look online, but just wanted to ask. Edit: Would something like this work… void Update() { Walk(); } void OnMove(InputValue value) { moveInput = value.Get(); } void Walk() { float velocityX = moveInput.x * walkSpeed; float velocityY = moveInput.y * walkSpeed; rb2d.velocity = new Vector2(velocityX, velocityY); } …or something like it? Would the Walk() be called in a FixedUpdate() instead?
@Dani_Krossing2 жыл бұрын
The new input system is a package you need to install, which comes with some cool benefits. 🙂 There is nothing wrong with programming the movement like this if you don’t plan on making the movement complex, or cross platform compatible. It is also a complete tutorial in itself to set up, which is why you will see KZbinrs do tutorials without it. Otherwise it would confuse more than what it benefits hehe.
@Dani_Krossing2 жыл бұрын
Walk() would need to go into a FixedUpdate() since you are using a rb2d component for movement. I wouldn’t include the rb2d.Velocity inside your update, since it would run even if there is no input or the input hasn’t changed. Which is just going to collect garbage. So I would do an extra check to see if the input has changed since last frame. Other than that, if you plan on using the new input system then you are headed in the right direction. 🙂
@scottisitt2 жыл бұрын
@@Dani_Krossing I see. Thanks!
@PolySh4pe Жыл бұрын
Awful tutorial. For example, at 6:14, you wrote if an brackets and it just made those little thingy's pop up. How???
@Dani_Krossing Жыл бұрын
This tutorial teaches how to create basic character movement. It’s not meant to teach the VERY basics of how to use Visual Studio. 🙂 If you struggle with basic VS shortcuts, then I recommend starting from the beginning, instead of jumping ahead.
@PolySh4pe Жыл бұрын
Nah man this comment was satire lel, i love how despite me being i prick you didnt get angry but instead handled it. new sub. :) @@Dani_Krossing
@cartermcknight40542 жыл бұрын
It keeps say "All complier errors have to be fixed before entering playmode" how do i fix this
@WarmFuzzlyBear2 жыл бұрын
Fix the error it should tell you what error you have on your code
@Chasealex11452 жыл бұрын
it keepgiving me errors and i have been trying this for four hours straight
@Dani_Krossing2 жыл бұрын
Like I replied in the first comment you made, I need to see the error to help you. 🙂
@CLAPNUGGETSdani Жыл бұрын
what did i do wrong using System.Collections; using System.Collections.Generic; using UnityEngine; public class playercontroller : MonoBehaviour { //components rigidbody2D rb; //player float walkspeed = 4f; float speedlimiter = 0.7f; float inputhorizontal; float inputvertical; // Start is called before the first frame update void Start() { rb = gameobject.getcomponent(); } // Update is called once per frame void Update() { inputhorizontal = input.getaxisraw("horizontal"); inputvertical = input.getaxisraw("vertical"); } } void fixedupdate() { if (inputhorizontal ! = 0 || inputvetical ! = 0) { rb.valocity = new vector2(inputhorizontal * walkspeed, inputvertical * walkspeed); } } }
@Dani_Krossing Жыл бұрын
Every line you wrote has at least one error in it. 😅 You didn't capitalize properly, you misspelled words, and you included empty spaces in places there shouldn't be any. Careful not being careless when following the tutorials, otherwise you will have a very painful time in the future doing constant bug fixing. 🙂 In this case here, I don't think it would be productive if I just told you where the errors are. I think you would get more out of trying to fix your code by going over it once more on your own... And if you then still can't figure out where the errors are, then you are more than welcome to ask for help again. 🙂