Moving Platforms in Unity

Objective: Camera adjustment and logic for moving platforms while making a primitive game.

Quick Camera Adjustment

To have the camera follow the player, make the camera a child of the player

Moving Platform

Moving Platforms using method MoveTowards : public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta);

Description: Calculate a position between the points specified by current and target, moving no farther than the distance specified by maxDistanceDelta.

Use the MoveTowards member to move an object at the current position toward the target position. By updating an object’s position each frame using the position calculated by this function, you can move it towards the target smoothly. Control the speed of movement with the maxDistanceDelta parameter. If the current position is already closer to the target than maxDistanceDelta, the value returned is equal to target; the new position does not overshoot target. To make sure that object speed is independent of frame rate, multiply the maxDistanceDelta value by Time.deltaTime (or Time.fixedDeltaTime in a FixedUpdate loop).

Note that if you set maxDistanceDelta to a negative value, this function returns a position in the opposite direction from the target.

Duplicate the platform you want to make move. Point_A = Starting Position | Point_B = Ending Position
After you get the ideal placement remove all components except the Transform.

Rename duplicated platform to Moving_Platform and add a new script MovingPlatform to its component.

MovingPlatform will have the following logic: Go to point b, if at point b, go to point a, if at point a, go to point b. This logic should be running in every frame(Update is called once per frame), therefore it will be in the update section.

Add variable fields for target a and target b positions.
_speed float for the moving platform
_switch bool for logic in moving from B to A and back.
Player and Platform issue. [L] Player does not stay on the platform.

Player Moving with Platform

Add a new box collider to the Moving Platform and edit it to make it a bit higher, additionally, you can change the color to red. Using the collider we will add a method to make sure our Player does not fall off when on the moving platform.

MovingPlatform new logic for player not to fall off: Collision detection with the player, what happens if we collide with the player, take the player parent to moving platform game object. Then also add exit collision, check if the player has exited the moving platform, and return the player to its hierarchy. This logic should only run when the player is on the moving platform (red platform) this will be OnTriggered.

Testing Player on Platform

If you want to know more about me, feel free to connect with me on LinkedIn

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store