I've searched everywhere for this answer but everything I find is in Javascript and a lot of it doesn't help/work in my situation since most of it is Vector3's and using each axis, and my game is 2D.

So I have these prefabs that get spawned off-screen and then I have a script attached to them that tells them to move left on the x axis. I need them to also pick a random direction (up or down) on the y axis, and travel that way until they get to a certain spot (range) and then to switch directions and go the opposite etc.

Here is what I have tried most recently:

public float itemUp = 0.1f;
public float itemMove = 0.7f;

public float x;
float y;
float z;
Vector3 pos;

void Update () {

    //transform.position = Vector2(Random.Range (1,4),Random.Range (1,4),10);

    transform.position -= Vector3.right * itemMove * Time.deltaTime;

    x = 0;
    y = Random.Range(-100, 100);
    z = 0;
    pos = new Vector3(-x, y, z);
    transform.position = pos * itemUp * Time.deltaTime;

Anyone know what I'm missing/not doing correctly? I'd really appreciate any help you can give, thank you.

  • \$\begingroup\$ What you want is to declare a random y_min and y_Max in the Start() function. And if transform.position. y is greater then or less then your y_min/y_max, reverse the direction. \$\endgroup\$ Jun 16, 2015 at 23:06
  • \$\begingroup\$ Alright, thank you, I get what you're saying, that shouldn't be an issue, but I'm having trouble with the random.range code when put into a transform.position function, do you know how that might look? Or if there is a different random function that needs to go there instead? \$\endgroup\$
    – S_Byrnes
    Jun 16, 2015 at 23:11
  • \$\begingroup\$ What determines when your objects change direction? What is currently happening that is not what you want? \$\endgroup\$
    – aslg
    Jun 16, 2015 at 23:16
  • \$\begingroup\$ I'm not 100% sure, I'm assuming it's the random.range code I have there under the y constraint, but it's really messed up, I can't find any good documentation on setting random variables and variable transform.positions anywhere, especially for C# like I need it to be. \$\endgroup\$
    – S_Byrnes
    Jun 16, 2015 at 23:34
  • \$\begingroup\$ @S_Byrnes I mean. What do you intend to do? Your object moves in a certain direction in the X axis, and you want it to simultaneously move in the Y axis until a certain point, then switch the direction again? (randomly) \$\endgroup\$
    – aslg
    Jun 16, 2015 at 23:56

1 Answer 1


Sample code to move an object up and down.

public float Y_Min = -100f; //Set this to the lowest Y value you want the Item to move to.
public float Y_Max = 100f; //Set this to the highest Y value you want the Item to move to.
public float VerticalSpeed = 5f; //Speed the Item will move vertically.
public float Dir = 1f; //Direction the Item is moving, either up or down.

void Start() {

    Dir = Random.value > 0.5f ? 1f : -1f; //Set Dir to start randomly either Up or Down.
    float StartY = Random.Range(Y_Min, Y_Max);//Get a random value between Min and Max to start off at.

    transform.position = new Vector3(transform.postion.x, StartY, transform.position.z);//Set item's Vertical position to a random value between Max and Min.

void Update () {

    //Check to see if the Item's Y position is above Max or below Min and switch direction.
    if (transform.position.y > Y_Max)
        Dir = -1f;
    else if (transform.position.y < Y_Min)
        Dir = 1f;

    //Compute new position based on VerticalSpeed and whether we are going up or down
    Vector3 NewPos = new Vector3(transform.position.x, transform.position.y + ((VerticalSpeed * Time.deltaTime) * Dir), transform.position.z);
    transform.position = NewPos;
  • \$\begingroup\$ Yeah, this would probably work for what I want, but only after I get the item to choose a random direction to go to first. \$\endgroup\$
    – S_Byrnes
    Jun 17, 2015 at 0:46
  • \$\begingroup\$ Updated answer to give a random starting position and direction. \$\endgroup\$ Jun 17, 2015 at 1:00
  • \$\begingroup\$ Thank you very much, this worked perfectly, I very much appreciate that, I didn't know of Random.value, so this has definitely opened my eyes. Oh, one thing there though, with your transform.position = new Vector3 line, the transform.position.x, the position is missing an 'i'. - Thanks again! \$\endgroup\$
    – S_Byrnes
    Jun 17, 2015 at 2:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .