Moving Platforms in Unity
Objective: Camera adjustment and logic for moving platforms while making a primitive game.
Quick Camera Adjustment
Description: Calculate a position between the points specified by
target, moving no farther than the distance specified by
Use the MoveTowards member to move an object at the
currentposition toward the
targetposition. 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
maxDistanceDeltaparameter. If the
currentposition is already closer to the
targetthan 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
maxDistanceDeltavalue 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
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.
Player Moving with 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.
If you want to know more about me, feel free to connect with me on LinkedIn