WASD Movement

How to make do basic WASD movement!

This page is unfinished, but made accessible because there may be useful content or links in the Notes section.

WASD movement in Unity is very simple. The only complexity is fixing diagonal speed, so let’s start.

Intro

We have to take the user’s Input (usually a number between -1 and 1) and add it to the GameObject’s current Position.

This will give us movement.

Making input update the position

  • Get Horizontal/Vertical Input values.
  • Add to the Transform’s position.
void Update()
{
	float horizontal = Input.GetAxis("Horizontal");
	float vertical = Input.GetAxis("Vertical");

	Vector3 forward = transform.forward * vertical * speed * Time.deltaTime;
	Vector3 right = transform.right * horizontal * speed * Time.deltaTime;

	cc.Move(forward + right);
}

Problem #1 - Relative forward

Problem #2 - Collisions

If we simply add the input to the Transform components Position, the GameObject will go through walls. It’s the same as if you were moving it inside the editor with the XYZ gizmo handles.

To fix this, we need to use a CharacterController component. Its Move() function will handle the complex collisions for us.

Problem #3 - Diagonal movement is too fast

Final code