I figure I can do/// while (transform.localPosition.y == arrayOfFloats[_targetHeight])
{
but what do I put here to make it re pick the random range value?
}
This away it doesn’t pick same number again and again.
How do I make random range more random? That and or make it not pick the same random one 40 times in a row.
Any help is welcome thanks.
Here is my code:
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
}