Moving a game object pausing the object then continue moving the object.

Hi everyone!

Im new to coding and was hoping that I could get a hand with a simple script that I am struggling with. I have an object that I want to move up and then pause before moving back down again. I am keen to do this in code rather then using Unity’s animation window. I have attached the code for you to have a look at. I think the problem is how I am using yield WaitForSeconds so any advice would be helpful.

Thanks!

var moveSpeed : float = 200;
var maxHeight : float = 200;

private var trans : Transform;
private var startingPos : float;
private var moveBackDown : boolean;

function Start ()
{
	trans = transform;
	startingPos = trans.position.y;
}
function Update (){
	if(moveBackDown)
	Upanddown();
}
function Upanddown ()
{
	if (trans.position.y <= maxHeight && moveBackDown == false)
	{
		trans.position.y += moveSpeed * Time.deltaTime;
	}
	
	if (trans.position.y >= maxHeight || moveBackDown == true)
yield WaitForSeconds(5);
	{
		moveBackDown = true;
		trans.position.y -= moveSpeed * Time.deltaTime;
		
		if (trans.position.y <= startingPos)
		{
			moveBackDown = false;
		}
	}
}

var totalMovementTime : float;
var startPosition : Vector3;
var endPosition : Vector3;
var speed : float;

private var lastPositionChange = 0.0;
private var currentVector : Vector3;
private var previousVector : Vector3;

function Start() {
	currentVector = endPosition;
	previousVector = startPosition;
	lastPositionChange = Time.time;
}

function Update() {
	transform.position = Vector3.Lerp(transform.position , currentVector, Time.deltaTime * speed);

	if(lastPositionChange + totalMovementTime < Time.time) {
		if(currentVector == endPosition) {
			currentVector = startPosition;
		}
		else {
			currentVector = endPosition;
		}
		lastPositionChange = Time.time;
	}
}

This code will go between startPosisition and endPosition at the set speed. You can set the total movement time to greater than that needed to go between the two points and it will wait there before moving on. There is one problem with this code which is that it will never truly reach it’s target position but it will get close enough that you wont really see the object moving.

Thank you for your reply. I tried your code and found that there is an easing in and out effect when animating between the two points. I was hoping to achieve more of a constant speed. Do you have any idea as to why the code I supplied does not work? Thanks for your help!

This code works fine for me :

var moveSpeed : float = 200;
var maxHeight : float = 200;

private var startingPos : float;
private var moveBackDown : boolean;

function Start () {
	//sets the position to come back to later on in the code
    startingPos = transform.position.y;
}

function Update () {
	//Checks to see which operation to perform - Travel up or Travel down
	if(moveBackDown)
    	Down();
    else 
    	Up();
}

function Up() {
	//checks to see if we have reached the maximum height yet
	if(transform.position.y >= maxHeight) {
		//wait for 5 seconds
		yield WaitForSeconds(5);
		//set the flag to allow the object to move back down
		moveBackDown = true;
		//Stops the function from porgressing any further
		return;
	}
	//applys the move speed to the y position of the object
	transform.position.y += moveSpeed * Time.deltaTime;
}

function Down() {
	//checks to see if we are beck to where we started
	if(transform.position.y <= startingPos) {
		//wait for 5 seconds
		yield WaitForSeconds(5);
		//set the flag to allow the object to move back up
		moveBackDown = false;
		//Stops the function from porgressing any further
		return;
	}
	//applys the same speed but downwards
	transform.position.y -= moveSpeed * Time.deltaTime;
}

One of the problems with your original work was you checked to see if moveBackDown is true before applying any functions, and booleans default at false… Your Upanddown functions was never run! Hopefully this code makes sense. I would reccomend setting your speed to slightly less than the max height as this will cause it to move there in one second.