Simple Player Movement in Unity

Full WASD/Arrow key Player Movement!

Objective: Get the cube to move horizontally and vertically using WASD.

The first thing we need to do is access the cube’s position in the world, and to do this, we can use:

transform.Translate

Transform is used to store an object’s position in the world, and Translate is what allows us to move the cube along a coordinate plane.

Now we need to access the X-axis and Y-axis of the coordinate plane. To do this, we can use:

new Vector2

With the new Vector2, we can assign an Input to allow the value of X and Y to increase and decrease with the press of the WASD/arrow keys on the keyboard.

Variables for moving the cube in a specific direction

Now we can move the cube left, right, up, and down! However, there is still a big problem:

Me holding down the right key

The cube moves at about… A million miles an hour!

This is because the cube is running at 1 unit per frame. To fix this, we need to make the cube move at 1 unit per second with:

Time.DeltaTime
Me holding down the right key

Much more manageable, but still way too slow for my liking. What we can do now is create a speed variable to give us the ability not only to increase the speed but set the speed to whatever we want.

Speed variable

Now the speed is right where I want it and if I wanted to change it more I can change it in the unity inspector.

Me holding down the right key
Changing the speed in the Unity inspector

Final code:

Final code

--

--

--

Learning to become a unity game developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Kubernetes- A rapidly growing technology!!

Widget testing with MockCubit

Obstacle Course Assignment Complete

How to use ipywidgets, interactive as used in the project

Devops is not an option for the telecommunications provider

[Live] UFC Fight Night: Hall vs Silva Live Stream Online Now[Live UFC Full Fight]

Insights into legacy software

We Need to Make Virtual Dev Conferences Better

single screen showing individual pictures of all attendees at 2020 CascadiaJS virtual conference

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tacoma Johnson

Tacoma Johnson

Learning to become a unity game developer

More from Medium

Vote for 3Moon

Thanks You To The A&Q Community For Participating In Ask Me Anything ( AMA ) With WhereAbout On…

LUDUFI INTRO