Quantcast
Channel: Questions in topic: "moving-platforms"
Viewing all articles
Browse latest Browse all 16

Help with my moving platform code.

$
0
0
Here is a vid of what is happening[link text][1] 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? [1]: https://www.youtube.com/watch?v=CSHeVmUXQ2M&feature=youtu.be 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 }

Viewing all articles
Browse latest Browse all 16

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>