Top Down Movement in Unity Tutorial
August 13, 2019
Unity Top Down Movement
Brackeys Tutorial
Youtube - TOP DOWN MOVEMENT in Unity!By: Brackeys
This seemed to fit well with the Pokemon tutorials I was doing recently so I decided to give it a look. It's been a while since I used Animator variables to control 2D animations so this was a nice refresher for that, but they also used Blend Trees which is something I had not seen before.
The initial script was very simple. This was a 2D Rigidbody setup, so it's more physics based than the Pokemon tutorial I am doing. As usual for physics in Unity, they do the physics work in a FixedUpdate method, however, they still receive the input in a standard Update method. I always see you should do your physics in FixedUpdate to deal with frame rate variance, but had not seen anything specific about splitting up the input and physics into the two different Updates.
The Animator starts by adding two simple parameters: float Horizontal and float Vertical. The Blend Tree happens in a single state in the Animator for the Animator Controller for the player. When you right click and select "Create State", you select "From New Blend Tree" instead of "Empty". You can double click it to go one level deeper into this state node. It's important to note the parameters apply here too, so they are useable throughout all the levels.
In this Blend Tree, there are three main factors to account for: the parameter, the thresholds, and the animations. You start with the parameter to check. You can then set "Motion Fields" which will hold an animation and parameter thresholds. It will then check the parameter relative to the thresholds set to determine which animation to play. This simple 2D walking animation set is a great example to show this off in a simple way, as it basically just switches between the 4 different directional walking animations depending on the combination of the horizontal and vertical parameters.
They then go back to the top level of the Animator to tie everything together. They make a transition from Idle to this Blend Tree, and vice a versa. This just makes sure to set a state for what to do when the player isn't putting in any inputs.
Finally, to tie this new Animator setup in with the actual movement script and player input, they go back to the PlayerMovement script. The small addition needed here is simply setting the Animator parameters equal to the input gotten in the Update method.
Overall, this was a neat way to approach 2D movement in Unity a bit differently than I have before. The Animator blend tree definitely seems nice for keeping the PlayerMovement script cleaner and easier to read instead of cluttering it with a lot of input comparisons to determine different sprites and animations to use.
Comments
Post a Comment