Moving object

I’m trying to make an object move up and down constantly. I guess I could animate it, but I want to use it for several objects and thought this might do it in javascript:

var starspeed:float = .02;
var extra:float = .5;
static var starwhere:float;

function Start() { starwhere = GameObject.Find(“Star”).transform.position.y; }

function Update () {
//if actual position y is greater equal to start y
if (GameObject.Find(“Star”).transform.position.y >= starwhere){
//move down
transform.position.y -= starspeed;
}

//if actual position y is less to start y
if (GameObject.Find(“Star”).transform.position.y <= starwhere + extra){
//move up
transform.position.y += starspeed;
}

}

The script should record the position of the object at the start and then move it down from that position a certain definable distance. When it hits that distance, it should move up. The object just sits there doing nothing. Thanks for looking.

A bare minimum approach. “” means “And”, and “||” means “Or”.

var moveSpeed : float = 1;
var maxHeight : float = 1;

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

function Start ()
{
	trans = transform;
	startingPos = trans.position.y;
}

function Update ()
{
	if (trans.position.y <= maxHeight  moveBackDown == false)
	{
		trans.position.y += moveSpeed * Time.deltaTime;
	}
	
	if (trans.position.y >= maxHeight || moveBackDown == true)
	{
		moveBackDown = true;
		trans.position.y -= moveSpeed * Time.deltaTime;
		
		if (trans.position.y <= startingPos)
		{
			moveBackDown = false;
		}
	}
}

Thanks theinformercial. I tried the code, but it did nothing too. Maybe it’s the way I have it set up. The best I could get was for it to move down but not back up. I got it to work with an animation and will go with that before I lose more hair. :slight_smile:

That’s impossible. You literally just drag the script i posted onto the object and it works. It’s supposed to move up first, then back down and it repeats the cycle. What on earth could you have done to not make it work?

http://dl.dropbox.com/u/10196246/vertical%20move.mp4

What can I say? Out of respect for your helping me in the first place, and less for your follow up, I tried it again on a fresh scene with the same results. Maybe you can see what on earth I’m doing wrong.

Yep, looks like I made an error in debugging my script. My bad!

The script is programmed to go to max height at the beginning. This is why it stops when it reaches max height (which would be 1). I tested the script with the object at the center of the world, rather than at any other height.

I’d edit it, but it seems you have a solution through animation (which is the approach I would have done as well). Good luck with your game!

Aha. Now I know that, I may try modifying your script. I need multiple powerups moving in a similar way and animation will mean animating each one…unless there’s a relative animation positioning method I don’t know about. Thanks!

It worked! Thanks again.