Right now im just using random range to do min height and max height but so far its not good enough I need specific coordinates so movement lines up properly with my model.
I was told to set up an array with a list of coordinates and have the random range pick from that but im not sure how to swap that out because im still new to coding this is what I got so far and I need 10 entry’s in the array for coordinates on the y axis.
For the most part I think I get it maybe just don’t know how to get the random range number to call the array number?
using UnityEngine;
using System.Collections;
public class Platform : MonoBehaviour
{
#region Fields
private float _targetHeight = 0f;
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]
[Range(9.99f, 25f)]
private float _heightMax = 10f;
[SerializeField]
[Range(1f, 9.99f)]
private float _heightMin = 1f;
[SerializeField]
private float _afterRaisePauseDuration = 2f;
[SerializeField]
private float _afterDropPauseDuration = 10f;
#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(_heightMin, _heightMax);
if (_randomizeSpeeds)
{
dropSpeed = _dropSpeed + Random.Range(0f, 3f);
raiseSpeed = _raiseSpeed + Random.Range(0f, 3f);
}
while (transform.localPosition.y < _targetHeight) // The loop that moves the platform towards the new target height.
{
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(_afterRaisePauseDuration); // pause before returning the platform to its original position, after the platform has lowered itself.
while (transform.localPosition.y > _originalPosition.y) // The loop that moves the platform towards the new target height.
{
targetHeightForThisFrame = transform.localPosition.y - Time.deltaTime * dropSpeed;
newLocalPos = new Vector3(_originalPosition.x, targetHeightForThisFrame, _originalPosition.z);
transform.localPosition = newLocalPos;
yield return null;
}
yield return new WaitForSeconds(_afterDropPauseDuration); // pause before running the loop again, after the platform has lowered itself.
}
}
}
#endregion
}