Timing my script with a stop watch, I see it is slightly out of sync. And can’t figure why. Is there anything I should be doing to keep it correct? I’ve attached the whole (unfinished) script in here, to put it all on context.
public class YPop : MonoBehaviour {
public float startTime = 1.0f;
public float moveAmount = 0.25f;
public float startHeight = -0.5f;
// public Collider Trigger;
public float beatTime = 60.0f;
float timer;
float rnd;
float oTime;
bool UpDown = false; //up true, down false
void Start(){
transform.position = new Vector3(transform.position.x, startHeight + transform.parent.position.y, transform.position.z);
}
void Update () {
if(timer >= 0.0f)
timer -= Time.deltaTime;
else{
StartCoroutine(WaitToPop(startTime));
timer = (60f / beatTime)*0.5f;
}
if(transform.position.y > startHeight + transform.parent.position.y)
transform.Translate(Vector3.down * Time.deltaTime);
}
IEnumerator WaitToPop(float startTime){
rnd = Random.Range(0.0f, moveAmount);
yield return new WaitForSeconds(startTime);
if(UpDown){
transform.Translate(Vector3.up * rnd);
UpDown = false;
}
else{
UpDown = true;
}
}
void OnCollisionStay(Collision col){
}
}