Platformer Features

Prefab Platforms, Using Cinemachine to Follow the Player, and Functioning Elevator.

James Lafritz
5 min readJun 24, 2021

Prefab

  • Create a modular prefab for the Platforms.

Cinemachine Camera Follow

  • Follow the player using Cinemachine Camera.

I installed The Cinemachine Package From the Package Manager.

Next I add A Virtual Camera to the Scene. I set the The Body to “Transposer” and the Aim to “Do nothing”. I then assign the object that I want to follow as the body. I the change the Offset. I do not want it offset on the Z Axis, this is the direction of travel for the Player/ I want it to Be Offset on the X Axis by 10, this moves the camera back Away from the Player. I also Move it Up on the Y Axis by 10.

The Camera is way to close to the Player so I adjust it slightly. I have to make sure that the camera is far enough away that the hand placement on the ledges isn’t noticeable.

I then changed the Body->Binding Mode to world space. This keeps the camera from flipping when the player turns and faces the other direction.

Elevator Lift

For a more complete Elevator see my article

  • Create a functioning lift system.
  • The platform should switch between floors with a 5 second delay at each level.

I edit my Lift Prefab.

I add A Smooth Path Game Object to the Platform of the Lift. I add a Cinemachine Smooth Path Component to the new GameObject. I set up My Paths, since this is only 2 floors I set the first point to (0, 0, 0) and the second point to (0, 34, 0).

I add a Cinemachine Dolly Cart Component to the Lift Floor. I set the Path to the Smooth Path I just created. I set the Update Method to Fixed Update. I set the Position Units to Path Units.

This rotated the Lift Floor -90 on the X Axis to aligning the GameObject with the path. To Fix this I rotated the individual Game Objects in the Lift Floor 90 on the X Axis. Now When I change the position units it moves the Lift up and down between the floors.

Now I need a Elevator Behavior attached as a Component to the Lift.

I need a speed to set the Elevator too. I also need a wait time so if the requirements change it is easy to do in the inspector. I also need a reference to Cinemachine Dolly Cart to get the position and to set the speed. I need to know what the next position should be, I also need to know the Direction that the elevator is moving and if the elevator is waiting. In the start method I make sure that there is a Dolly Cart Assigned. I then set the speed of the Dolly Cart to 0 so it is not moving. I then make sure the Dolly Cart is set up correctly.

I need a way to set the Next Position that the Platform should move to. I take the next position and increase it by the current direction, if I am going up this will be 0, 1, 2, etc if going down this will be 2, 1, 0. Next I check if the Next Position is outside the bounds of the path, if it is I change direction and then set the next position to the correct position, the multiplication is there because I need to increase or decrease the Next Position if I did it once I would get the current position.

Since I need to wait before moving the elevator to the next floor I need to use a Coroutine. I set the Speed of the Platform to 0, this stops the elevator moving. I then set the Is Waiting to true. I Get the Next Position, I then wait for the Wait time. Now the Elevator should be Moving so I set the Platform Speed to the Speed to move multiplied by the Direction that it should be moving. I then Indicate that the Elevator is no longer waiting.

Now in the Update Method I exit the Method is the Elevator is waiting. I then check the direction that I am moving. If I am moving up then I check to see if the Platfrom’s current position is equal to or greater then the Next Position, If it is I have reached the next floor so I start the Move to Next floor. If the Elevator is moving down then I do the same, the current position is Less then or equal to the Next Position means that I have reached the floor.

I edit the Prefab and assign the Elevator’s Platform. The I run the Game and make sure that it works as expected.

--

--

James Lafritz
James Lafritz

Written by James Lafritz

Excited about changing my hobby into a new carer with GameDevHQ course.

No responses yet