Puzzled over my 2D vertical moving platform ( while loop error )

Hey all

Yet another section of my coding has gone pear shaped and my logical skills are lacking.

I am trying to make a moving platform which moves up - the y axis - for a bit, stops and continues to declines to the start position

   // Set the position to start the platform moving from
function Start()
{
transform.position.y = 111;
}

function FixedUpdate () 
{
MoveUp();
MoveDownt();
}


function MoveUp () 
{
while(transform.position.y<120)
{
transform.position.y +=0.0025;
}
}

function MoveDown ()
{
yield WaitForSeconds (5);
while(transform.position.y!=110)
{
transform.position.y -= 0.0025;
}
}

I cant figure out how to code this as if it is not equal to 110 but is also less than 120 then its obv gonna crash with an infinite loop

please can anyone offer any things to try

Cheers

var upper : float = 120;
var lower : float = 110;
var speed : float = 1.0;

function Start() {
  Move();
}

function Move() {
  while ( true ) {
    MoveUp();
    MoveDown();
  }
}

function MoveUp() {
  while ( transform.position.z < upper ) {
    transform.position.z += Time.deltaTime * speed;
    yield;
  }
}

function MoveDown() {
  while ( transform.position.z > lower ) {
    transform.position.z -= Time.deltaTime * speed;
    yield;
  }
}

thanks for the quick response

i see whats going on there - thts great

however unity crashes when I execute the scene :S

It’s supposed to be “yield MoveUp()” and “yield MoveDown”; my bad.

I thought I’d write up a script for you. I didn’t think it’d be super complex, but it uses my mapping function which is so amazing for things like this.

var startPos:Vector3 = Vector3.zero; //Set these for certain.
var endPos:Vector3 = Vector3.zero;

var moveDuration:float = 5; //(in seconds)
private var moveDurationValue:float = 0;
private var moveDirection:boolean = true;

var delayDuration:float = 5;
private var delayDurationValue:float = 0;
private var delaying:boolean = false;

function Update()
{
      if(delaying)
      {
            delayDurationValue += Time.deltaTime;
            if(delayDurationValue >= delayDuration)
            {
                  delaying=false;
                  moveDirection = !moveDirection;
				  delayDurationValue = 0;
            }
      }else{
            if(moveDirection)
			{
				moveDurationValue += Time.deltaTime;
				if(moveDurationValue >= moveDuration)
				{
					delaying = true;
				}
			}else{
				moveDurationValue -= Time.deltaTime;
				if(moveDurationValue <= 0)
				{
					delaying = true;
				}
			}
			transform.position.x = Map(moveDurationValue, 0, moveDuration, startPos.x, endPos.x);
			transform.position.y = Map(moveDurationValue, 0, moveDuration, startPos.y, endPos.y);
			transform.position.z = Map(moveDurationValue, 0, moveDuration, startPos.z, endPos.z);
      }
}

function Map(value:float, istart:float, istop:float, ostart:float, ostop:float)
{
 	return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
 }

Simple as attaching the script to the gameobject you want to move, then set the start pos and end pos, and finally decide on your movement time and your delay time. System works flawlessly. (I already tested it with a basic cube in Unity 3.1)

Hope this helps you. It may seem a little complex, but it’s extremely scalable.

Oh, and since your doing it in 2d, here is a vector2 version:

var startPos:Vector2 = Vector2.zero; //Set these for certain.
var endPos:Vector2 = Vector2.zero;

var moveDuration:float = 5; //(in seconds)
private var moveDurationValue:float = 0;
private var moveDirection:boolean = true;

var delayDuration:float = 5;
private var delayDurationValue:float = 0;
private var delaying:boolean = false;

function Update()
{
      if(delaying)
      {
            delayDurationValue += Time.deltaTime;
            if(delayDurationValue >= delayDuration)
            {
                  delaying=false;
                  moveDirection = !moveDirection;
				  delayDurationValue = 0;
            }
      }else{
            if(moveDirection)
		{
			moveDurationValue += Time.deltaTime;
			if(moveDurationValue >= moveDuration)
			{
				delaying = true;
			}
		}else{
			moveDurationValue -= Time.deltaTime;
			if(moveDurationValue <= 0)
			{
				delaying = true;
			}
		}
		transform.position.x = Map(moveDurationValue, 0, moveDuration, startPos.x, endPos.x);
		transform.position.y = Map(moveDurationValue, 0, moveDuration, startPos.y, endPos.y);
      }
}

function Map(value:float, istart:float, istop:float, ostart:float, ostop:float)
{
 	return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
 }

You can look through the code to under stand it, but it’s scripted in a way that you don’t need to even look at the code. :slight_smile: Yay for easy to use crap!!!