Unity Movement Basics Tutorial

October 6, 2020

Unity

Movement


Title: Move in Unity3D - Ultimate Unity Tutorial
By: Jason Weimann
Youtube - Tutorial
Description: Tutorial for covering the basics of all the different ways to move objects in Unity.


Overview

Movement is something I have worked with a lot in Unity as it is part of basically every project in some way or another. Being such a prevalent topic, I just wanted to make sure I had a good understanding of all the different ways to apply movement so I could pick the best options for specific projects. This tutorial is very basic on every front, but inclusive so I figured it was a good point to make sure I knew all the options. There was not a lot to learn from this tutorial for me, but it did help support some of the approaches I already use.

Tutorial Notes

Transform Based Movement

These approaches to movement deal with directly altering the transform values of an object, such as position and rotation, mathematically through general vector math. This generally the built in physics system and gives very tight and precise controls.

Move Transform Position With Input

This is the first basic approach of altering the transform.position value of objects directly. This used GetKeyDown (which only applies once per press) along with large value changes to create more segmented or tile based movement.

Move Transform Position With Input Limit Movement

Similar to the previous approach but it used a mathematical position check to limit the player's movement. This kept the player's positional value from getting too large or too small, which effectively creates boundaries without creating physical boundaries within the game.

Move Transform Position With Input Limit Movement Cleaned

This had the same exact logic as the previous approach, but used different syntax to be more readable. This included a strong focus on using expression body property methods to assign bool variables all within a single line of code.
Example:
private bool ShouldMoveBack => Input.GetKeyDown(KeyCode.DownArrow) && transform.position.z > -1;

Move Transform Position With Input Smoothly

This was the first approach to apply Time.deltaTime to the transform.position value to create much smoother movement. They swapped over the GetKey instead here to continuously apply the movement while the key is pressed. As usual they also applied a move speed value within the movement to help control the size of the incremental steps, and in turn the general speed of the object.

Move Transform Position With Input Smoothly And Rotation

Similar to the previous approach but they added rotation functionality. This was done simply by using Unity's Rotate method along with a rotation speed value and Time.deltaTime to keep it smooth as well.

Force Based Movement

Move With Force

This started the use of the rigidbody component, which is necessary to use Unity's physics system. They simply started with the rigidbody.AddForce method to apply force to the object within the physics system and cause it to move. Applying this to a cube however caused it to topple over and give awkward results since the force direction changed with the rotation of the cube.

Move With Force Use Tranform Rotation

To fix some of the issues they were having, they froze the rotation of the rigidbody on all axes so it could not rotate at all. Now when the force was applied the cube simply slid along the ground in the same configuration at all times. To allow it to rotate along the vertical axis of the world, they simply applied an internal transform.Rotate method.

Jump

They simply allowed a strong upwards force to be created with the AddForce method. This section was interesting as they once again covered the benefit of separating your player input reading into your Update method, while your physics application is done in FixedUpdate. They accomplished this by having the inputs in Update simply set bool values, and then FixedUpdate would read those bool values to determine if they should perform an action and what action it should be, and then the FixedUpdate would reset the bools if they performed their action. This helps keep the physics running consistently and smoothly while removing the possibility of inputs being missed or eaten by the player.

Move With Force Transform Rotation Clamp Velocity

This option prevented force from being able to be applied when the velocity of the object passed a certain value. This however is a weird soft cap, because force and velocity are not extremely tied together. Force applies an acceleration which in turn alters the velocity of the object. This means that the velocity can hit a range of different values passed the cap (hence the soft cap) depending on how much force is applied in the instances leading up to the object reaching the velocity cap.

Character Controller

While this is mostly tied to rigidbody components, it felt like its own varied option for movement. This is a component provided by Unity which helps get the basics of a player controller going for you. Along with the basics of setting up movement, it also has some additional features such as slope detection and elevation/step detection.

Navigation Mesh

They really just covered the bare minimums of using a Navigation Mesh to help you move a character around. This also is not something I am focused on using in my next major project so I did not need this section for now.

Animation

This focused on how you can use Unity's Animation window to provide movement to objects. You create an animation for an object, and then you can select "Record" and save various parameters of the object at various times as key frames for an animation. While the focus was on changing the object's transform.position values to animate movement, this can be done with any values such as scale or color even. This does not give a lot of variability, but can be useful to quickly get something moving with a repeating pattern in the environment for example.

Summary

As expected there was not a whole lot of new information I got from this tutorial, but it did help me organize the different options in my head better. It also provided yet another point of support on the separation of input and application to movement through the Update and FixedUpdate methods again in the Jump section so that was good to see. That separation is definitely something I will be using moving forward with my own projects.

Comments

Popular posts from this blog

Online Multiplayer Networking Solution Tutorial Using Unity and Mirror - Tutorial by: Jason Weimann

Exporting FBX from Houdini with Color

Houdini Assignment - Fuse and Group