Moving Cube Buddy around in Unity
For today’s article, I’ll be going over how I add controls to move a cube around Unity using the wasd or arrow keys.
Final Result
To get to the above end result, I created a new script named Player.cs
that contains the following logic below:
To change the default inputs set by Unity’s Input Manager, go to Edit -> Project Settings -> Input Manager
What is Time.deltaTime?
Time.deltaTime is the amount of time between the last frame and current frame measured in seconds. This is used to convert the Cube’s movement from Units per Frame to Units per Second.
What is .normalized?
Based on Unity’s Documentation, normalized returns a “vector with a magnitude of 1”. What does that mean and why is that important here? By setting the direction the player moves to a unit vector or distance of 1, it prevents the player from moving faster when moving diagonally. In other words, the same speed it moves horizontally or vertically will be the same when it moves diagonally. If you want to learn more about unit vectors mathematically, please refer to Khan Academy videos here.
Thanks for reading :)