Moving Platform Part 2

Using Cinemachine Smooth Path

James Lafritz
Dev Genius

--

Cinemachine is a powerful suite of tools that can be used for creating your cameras. It does everything from blending between cameras, tracking objects in the scene, and camera effects. It is a one stop solution for everything camera related. If there is a camera in the game I recommend using Cinemachine. Since I was planing on using Cinemachine in my game I thought that I would make use of it’s features of moving a camera along a path to move my game objects along a path. I like the clean look of the paths in the editor. Cinemachine has 2 different path types that can be used, Cinemachine Path and Cinemachine Smooth Path. Cinemachine also provides Cinemachine Dolly Cart which use the path to constrain a Game Object transform on the path and can be used to animate it to move along the path.

Installing Cinemachine

To install Cinemachine I have to do so from the Package Manager.

Window->Package Manager

I want to change to the Unity Registry and then start typing Cinemachine in the search bar.

Once I find it I click install. The current version available to me is 2.7.3

Setting up the prefabs

I have duplicated the Moving Platform Prefabs. I am going to use Cinemachine to move the Platform along the Path. I also need a Behavior that inherits from Dolly Cart.

I am going to remove the Moving Platform Component and Add a Moving Platform Dolly Cart Component to the moving platform part. I change the Position Units to Normalized, this will make it so no matter how many points in the path there are the position is clamped between 0 (the first point) and 1 (the last point). I also set the speed to 1. I also forgot that I want the Update Method to be Fixed Update.

I then edit the Moving Platform Path Prefab. I add a Cinemachine Smooth Path to the Target Path, I can use either of the Cinemachine paths. I remove the second Waypoint, and zero out all of the first way points position. I assign the Target Path to the path for the Moving Platform. I then removed the to Target Transforms as they are not needed.

Next I add way point to may path. Notice that when points are added to the platform rotated so that it faces the path, if I rotate the path it rotates to always be pointing along the path.

Now when I run the game the platform will move along the path at the speed that is set in the inspector. If I change the path to be looped it will move from the last position to the first position.

Fixing the Platforms

To fix the Platform Issue All I have to do is rotate the original platform prefab 90 on the Y axis and change the scale to be 2 on the X and 5 on the Z. I did go and double check that all of the platform prefabs updated, just in case I changed this value some place along the way and it was saved in the variant as a change. The Platform that moves using Cinemachine rotates itself to orient it along the path.

Now all of the platforms in the Game are rotated and display the same.

Making the Player Move with the Platform

Since it is possible for the player to stand on any side of the platform I have to adjust the trigger collider so it detects the player on all sides.

Now all I need to do is copy the On Trigger Enter and On Trigger Exit code from the Moving Platform to the Moving Platform Dolly Cart.

Now the Player will Move with the Platform. Eventually dumping the player off as gravity kicks in.

The Position Units

Depending on what the Position Units are set to depends on how the platform moves along the path. Distance the Speed controls how fast it moves along the distance. Path Units Speed controls how fast it moves from one way point to the next. Normalized Speed controls how fast it moves from 0 to 1.

Modifying the Path

Now I can get all kinds of different results just by changing the path that it follows, whether it is looping or not and by changing the speed. The possibilities are endless on what I can do with the moving platform especially with Triggers and Unity Events. I can have the speed at 0 until the player lands on it and then it starts moving. I can have a switch someplace that enables the game object and it will start moving. I can make it pin pong automatically by having points that match the first set in revers and set it to is looping. I have complete control over it without having to add any lines of code. Making this a much more versatile solution then using

Separating the logic

Since the setting of the parent is different from the logic of the moving platform it is best to separate this logic out to a new Behavior. This allows Me to completely remove the custom Move Platform Track Dolly and allows me to decide if I want this behavior or not. As making the Player a parent of this object may cause some undesired effects.

I Deleted the Moving Platform Dolly Track, Created a new Move Player With Platform Behavior. I Put the Trigger Enter and Trigger Exit code into the new Behavior. I removed the Trigger Enter and Trigger Exit Code from the Moving Platform. I then added the new Move Player with Platform to the Moving Platform Part Prefab. I also Added the Cinemachine Dolly Track Component to the Moving Platform Path.

Fixing the player rotating weirdly

Instead of setting the player’s parent I am going to make the Player Move with this object. I change the set parent code to caching the player’s transform. I add a Check to see if there is an other transform if not I exit out of the Method.

In the Fixed Update Method I set the players position to this position. If I am still in the Fixed Update Method then there is another transform.

Now this Works but not quite as desired.

The trick is to move the other transform by the amount the platform moved. to get the change in position I take the platforms current position minus the platforms last position. I add this amount to the players position. Then I set the platforms last position to the platforms current position. I make sure that when the Player enter the trigger I set the platforms last position to the position that the platform is at.

Now my player moves with the platform and I don’t get any weird rotations applied to the player.

--

--

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