Pausing a Moving Platform using C#

I’m new to Unity and don’t know C# very well. I found this C# script for moving a platform between an Origin and Destination point, but I don’t know how to code in a pause so the player has time to get on/off the platform. I assume the code will go where it Switches between true and false, but I don’t know how to do it. Help is very much appreciated. Here’s my code that’s attached to the platform.

using UnityEngine;
using System.Collections;

public class MovingPlatform : MonoBehaviour
{
public Transform DestinationSpot;
public Transform OriginSpot;
public float Speed;
public bool Switch = false;
public float seconds = 5;

void FixedUpdate()
{
	// For these 2 if statements, it's checking the position of the platform.
	// If it's at the destination spot, it sets Switch to true.
	if(transform.position == DestinationSpot.position)
	{
		Switch = true;
	
	}
	if (transform.position == OriginSpot.position) 
	{
		Switch = false;

	}
	
	// If Switch becomes true, it tells the platform to move to its Origin.
	if(Switch)
	{
		transform.position = Vector3.MoveTowards(transform.position, OriginSpot.position, Speed);
	}
	else
	{
		// If Switch is false, it tells the platform to move to the destination.
		transform.position = Vector3.MoveTowards(transform.position, DestinationSpot.position, Speed);
	}
}

}

Add an extra variable like…

float waitUntilTime = -1f; //-1 means don't wait.

When you want to pause the movement action, set that value to be a little bit in the future, like so:

waitUntilTime = Time.time + 3f; //3 seconds in the future.

Then add a check at the beginning of your update method. If you’re still waiting, then exit early.

if (Time.time < waitUntilTime) return;

I don’t want to write all the code for you, but I think maybe you can understand what to do from the above.

The code you would need to make the platform pause would look like this:

using UnityEngine; 
using System.Collections;

public class MovingPlatform : MonoBehaviour 
{ 
	public Transform destinationSpot; 
	public Transform originSpot; 
	public float speed = 5.0f;  
	public float pauseDuration = 5.0f;

	private float platformTimer = 0.0f;
	private bool Switch = false;
	
	void FixedUpdate()
	{
		// For these 2 if statements, it's checking the position of the platform.
		// If it's at the destination spot, it sets Switch to true.
		if(transform.position == destinationSpot.position && Switch == false)
		{
			Switch = true;
			platformTimer = 0.0f;
			
		}
		if (transform.position == originSpot.position && Switch == true) 
		{
			Switch = false;
			platformTimer = 0.0f;
			
		}
		
		// If Switch becomes true, it tells the platform to move to its Origin.
		if(Switch)
		{
			// If the platformTimer is more than the pauseDuration then start 
			// moving the platform else make the platformTImer count up.
			if(platformTimer >= pauseDuration)
			{
				transform.position = Vector3.MoveTowards(transform.position, originSpot.position, speed * Time.deltaTime);
			}else
			{
				platformTimer += Time.deltaTime;
			}
		}
		else
		{
			// If Switch is false, it tells the platform to move to the destination.

			// If the platformTimer is more than the pauseDuration then start 
			// moving the platform else make the platformTImer count up.
			if(platformTimer >= pauseDuration)
			{
				transform.position = Vector3.MoveTowards(transform.position, destinationSpot.position, speed * Time.deltaTime);
			}else
			{
				platformTimer += Time.deltaTime;
			}
		}
	}
}

I’ve added a ‘platformTimer’ and a ‘pauseDuration’ variable to your script. In the ‘if(Switch)…’ if statement it will check to see if the ‘platformTimer’ variable’s value has reached the value of ‘pauseDuration’. If so then it will start moving the platform. This way, a delay is created each time the platform reaches the direction it is going in.

Also notice that I’ve changed the last parameter of the ‘MoveTowards’ function from just ‘speed’ to ‘speed *Time.deltaTime’. Now the platform will move by the amount of the speed variable per second instead of per frame. This way, the platform will always move at the same speed even if your game’s frame rate changes for any reason. It’s for the same reason that I add ‘Time.deltaTime’ to the ‘platformTimer’ variable too, instead of just a number.