Help with my moving platform code.

Here is a vid of what is happeninglink text sorry for the lag I guess it doesn’t like fraps.

The platforms go to their first random spot then once they are there they just bounce in place and because they keep moving they don’t finish the loop to go to another random position.

Any idea on how to stop them from bouncing like this and to keep them going through the loop?

using UnityEngine;
using System.Collections;

public class Platform : MonoBehaviour
{
	#region Fields
	private int _targetHeight = 0;
	
	private Vector3 _originalPosition;
	#endregion
	
	#region Inspector Properties
	[SerializeField]
	[Range(0.25f, 10f)]
	private float _raiseSpeed = 1f;
	
	[SerializeField]
	[Range(0.25f, 10f)]
	private float _dropSpeed = 0.5f;

	[SerializeField]
	private bool _randomizeSpeeds = false;
	
	[SerializeField]
	private float _waitPauseDuration = 10f;
	
	public float[] arrayOfFloats;
	
	public void Awake()
	{
		arrayOfFloats = new float[10];
		arrayOfFloats[0] = 98.197f;
		arrayOfFloats[1] = 99.188f;
		arrayOfFloats[2] = 100.182f;
		arrayOfFloats[3] = 101.173f;
		arrayOfFloats[4] = 102.1631f;
		arrayOfFloats[5] = 103.156f;
		arrayOfFloats[6] = 104.147f;
		arrayOfFloats[7] = 105.138f;
		arrayOfFloats[8] = 106.130f;
		arrayOfFloats[9] = 107.118f;
	}
	#endregion
	
	#region Initialization
	private void Start ()
	{
		if (transform != null) {
			_originalPosition = transform.localPosition;
			StartCoroutine(PlatformMotionLoop());
		}
	}
	#endregion
	
	#region Motion Handling
	private IEnumerator PlatformMotionLoop()
	{
		float targetHeightForThisFrame = 0f;
		Vector3 newLocalPos = Vector3.zero;
		float raiseSpeed = _raiseSpeed;
		float dropSpeed = _dropSpeed;
		
		if (transform != null)
		{
			while (true)
			{
				_targetHeight = Random.Range(0, arrayOfFloats.Length);
				
				if (_randomizeSpeeds)
				{
					dropSpeed = _dropSpeed + Random.Range(0f, 3f);
					raiseSpeed = _raiseSpeed + Random.Range(0f, 3f);
				}
				
				while (transform.localPosition.y != arrayOfFloats[_targetHeight]) // The loop that moves the platform towards the new target height.
				{
					if (transform.localPosition.y < arrayOfFloats[_targetHeight])
					{
						targetHeightForThisFrame = transform.localPosition.y + Time.deltaTime * raiseSpeed;
					}
					else if (transform.localPosition.y > arrayOfFloats[_targetHeight])
					{
						targetHeightForThisFrame = transform.localPosition.y - Time.deltaTime * raiseSpeed;
					}
					newLocalPos = new Vector3(_originalPosition.x, targetHeightForThisFrame, _originalPosition.z);
					
					transform.localPosition = newLocalPos;

					
					yield return null;
				}
				
				yield return new WaitForSeconds(_waitPauseDuration); // pause before returning the platform to its original position, after the platform has lowered itself.
			
				
			}
		}
	}
	#endregion
}

This is because the transform.localPosition.y will never (extremely unlikely at least) be equal to your target value.

Lets look at what you have:

targetHeightForThisFrame = transform.localPosition.y + Time.deltaTime * raiseSpeed;

Here you set the value to be equal to the current y position and add Time.deltaTime multiplied by
raiseSpeed. Lets say the current y position is 98.19 (just below your first value in the array) and add Time.deltaTime (which is usually around 0.02) multiplied by raiseSpeed (which is a default value plus a random value between 0 and 3, lets say: 4.756). This turns the math into:

value = 98.19 + 0.02 * 4.756 = 98.28512

98.28512 is above the first target position.

Now with floating point numbers like this, what is the likelihood of that value to ever be exactly equal to your target value? It is almost impossible. Even if the value became equal at some point, it would still not be exactly equal due to floating point imprecision.

You should check for distance to the targetposition instead and have a threshold where it is good enough for you. This threshold can be small (around 0.1 for example).

Example:

if(Mathf.Abs(currentPosition - targetPosition) < thresholdDistance)
{
    //Done
}

Even better though would be to not rely on checks like this and rather move the object specifically from the startposition to the targetposition using Mathf.Lerp, or any type of tweening library (for example: iTween).