# Random Y Axis Movement Within Set Limits (C#)

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.

• 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. – Jeremiah Leslie Jun 16 '15 at 23:06
• 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? – S_Byrnes Jun 16 '15 at 23:11
• What determines when your objects change direction? What is currently happening that is not what you want? – aslg Jun 16 '15 at 23:16
• 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. – S_Byrnes Jun 16 '15 at 23:34
• @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) – aslg Jun 16 '15 at 23:56

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;
}

• 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. – S_Byrnes Jun 17 '15 at 0:46
• Updated answer to give a random starting position and direction. – Jeremiah Leslie Jun 17 '15 at 1:00
• 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! – S_Byrnes Jun 17 '15 at 2:09