Moving platforms in Unity
Objective: Move a platform between two points
Moving a platform between two positions can be very simple. In the above GIF I have a platform prefab that is moving between to points, these two points can be moved wherever you want and the platform will perform the same. It will make its way towards one point and then go back to the other one.
I first created a 3D cube in the hierarchy and then changed the scale for the size of platform that I required. I then reset the transform to be 0,0,0. To keep the platform contained I created an empty game object and renamed it to movingPlatform. I moved the cube I made into the newly made empty object and reset the transform so that it is at 0,0,0.
On the platform(cube), I then created an attached a script called MovingPlatform and added in a rigidbody component and unticked gravity.
Now comes the setup for the MovingPlatform script.
I have set up the two positions that I want the platform to move between, the speed for the platforms to move and a bool for switching the direction of the platform.
Because this is a physics movement, in Unity it is best to used FixedUpdate instead of update. This means that everything happens at a fixed rate and is not dependent on the players computer speed.
The above code can be more concise for those of you who like that sort of thing.
Vector3 targetPosition = _switching ? _targetA.position : _targetB.position;
transform.position = Vector3.MoveTowards(transform.position, targetPosition, _speed * Time.fixedDeltaTime);
if (transform.position == _targetB.position)
_switching = true;
else if (transform.position == _targetA.position)
_switching = false;
I have two if/else statements here that are handling two different situations.
The first statement is controlling the actual movement of the platform, the second is determining whether the platform has reached one of the targetPositions.
- Statement 1
if the switching bool is false then moveTowards target b position, else if switching is true, moveTowards target a position. - Statement 2
if the current position of the platform is equal to that of the targetB position then switching is true, else switching is false.
With the MoveTowards method, this will move an object between two points, you control the speed by using the third parameter, and because we’re in a FixedUpdate, it is best to use Time.fixedDeltaTime.
Now save the script and hop back into Unity. As you have seen in the script I made three of the variables a serialized field it can be view in the inspector. Now create your two points as children of the moving platform game object. Don’t forget to reset the transform.
Now duplicate the first point object and move it to another position. Once you have your position objects, you can add them to the movingPlatform script that should be on the platform. Now set your speed and you should be good to go and test.
Press play and you should now see the platform moving between your two points.
To end the article, I would also recommend making this a prefab. For this I would reset the transforms of the parent container, the platform and the positions. This way when a designer comes along and wants a platform they can just drag in the prefab and move the points to where they want for that platform.