When Life Knocks You Down…

Person in the air falling backwards in an office setting

Ask why your developer let you topple over in the first place.

In this post, we will look at how to keep your character upright and other worthwhile reflections on character movement in Unity.

Welcome back! How are things going on the project, you ask? Well, the team is currently neck deep in developing the tutorial room and working with on the global features of our escape room game. Ted was focused on writing an intial script for player movement and camera panning, but ran into some issues and asked for an assist on getting some of the glitches worked out.

Our design doesn’t use a physical player, but the camera perspective. Our goal was to be able to rotate on the left and right arrow and go forward and backward based on the direction the camera is facing. Additionally, we want the camera to be able to pan on the x and y axes.

Issues to be addressed:

  • Player Capsule Tumbling – since our player isn’t supposed to be a gymnast, we wanted it to be upright.
  • Camera Clipping – on the edges of the room we would get flashes of the scene outside
  • No Player Rotation – player moved on x and y axes instead of rotating on y axis and moving on z axis
  • Global Direction for Player Movement – the player movement direction was aligned to the world and not to the perspective of the player

Keeping an Upright Character

The fix for our player gymnastics was very simple. It just required checking a couple boxes in the “Constraints” section of the “Rigid Body” component of the capsule we used for the player.

Camera Clipping

This was another very simple fix that involved reducing the “Clipping Planes” field value for the camera.

Player Rotation and Global Direction for Player Movement

This was slightly more complicated and involved attaching a script to our player capsule. These functions are interrelated. The code we used in our update function is broken down below.

Left and Right Rotation of Player

float horizontal = Input.GetAxis("Horizontal") * 
    RotationSpeed;
transform.Rotate(0, horizontal * Time.deltaTime, 0);

Input.GetAxis checks for a particular input and in this case, if the left or right keys pressed. The keys associated with Horizontal and other arguments can be found and changed in Edit >> Project Settings >> Input Manager >> Axes.

transform moves the object and in this case rotates it left and right based on the input.

Time.deltaTime makes sure the speed of the movement is the same regardless of hardware.

Forward and Backward Movement Based on Rotation of Player

Vector3 vertical = Vector3.zero;
vertical.z = Input.GetAxis("Vertical") * MovementSpeed;
moveValues = transform.TransformDirection(vertical);
characterController.Move(moveValues * Time.deltaTime);

Input.GetAxis here checks for the forward and backward keys because that is what is currently associated with Vertical in the Input Manager.

transform is used here again with TransformDirection. This changes the x, y, z axes to the local space which in this case is the player’s perspective from the world space or global space.

characterController.Move Moves the character based on a Vector3 passed as an argument. In this case, it is just forward and backward since we have zeroed out the x and y directions.

(Camera) Perspective and Wrap Up

With the help of the sources listed below and some time, we were able to get the player movement sorted out. Getting the camera to take the perspective of the player was as simple as making the camera a child of the player capsule and making sure the projection was on perspective. Integrating camera panning though, that’s a story for another time.

Until the next post, be a goldfish.

Sources:

Print Friendly, PDF & Email