Creating a Zombie Shooter — Setting things up and making a physics-based player controller.

Esteban Ibarra
4 min readSep 7, 2021

After starting a new Unity project, we created a cube that we stretched to 20x,0,20z and added a capsule for the player. To bring a bit of moodiness, we changed the lighting to a more spooky blue.

Next, like in the 2.5D course, we created a physics based player controller:

First we’ll create the player script and add it to the Player object, and then we’ll add a character controller component as well.

We were given the challenge to write the character controller with some tips on how to solve it:

//get handle to character controller.
//WASD Keys for movement
//input system (horizontal, vertical)

//direction = vector to move
//velocity = direction * speed

//if jump
//velocity = new velocity with added y.

//controller.move(velocity * time.delta)

Sounds simple enough! Let’s program it!

Get handle to character controller:

It’s best practices to nullcheck any component so let’s create something that will alert our developers if character controller isn’t assigned.

We’re going to need a speed variable so that our player doesn’t scoot forward like a person waiting in line at the DMV.

Let’s throw in a jump variable for good measure:

And if we’re going to jump, let’s add some gravity:

We ran into some issues last time with our character controller due to variables being zeroed out every frame so let’s create a holder for velocity y.

WASD Movement

Unity has built in WASD movement via the horizontal and vertical input axis so let’s just use those:

Direction = Vector to move

We put the vertical input in the z-axis so up and down gets translated to forward and backwards.

Velocity:

Now that we created velocity, we can modify it for jumping:

First, we check if the character is grounded, the character will only be able to jump if they’re on the ground. if they are, we’ll check for the jump button/spacebar, and once done, we’ll assign _jumpHeight to the _yVelocity temporary variable holder.

next, we’ll allow gravity to do its thing, and finally assign _yVelocity to the actual velocity variable.

Controller.Move:

Altogether now!

and now, the moment of truth!

Working perfectly! Tomorrow we’ll get into cameras!

--

--