- Home /
Move to random position based off user's input of terrain size?
We need a way for our rain to randomly move around the parameters of the user's terrain size, which is inputted by the user.
How can we make it so that the rain will randomly move across the terrain based on time.
Example (which will be hard to explain): The rain ellipsoid is set at 500x500 and the terrain size is 5000x5000 the rain is moving randomly based off the user's input of the terrain size 5000x5000. Since the rain is 500x500 moving at a few degrees a second it can only cover a parameter of 500x500 at a time. So it can move itself across the terrain over time to 2500x2500 then over more time 1000x1000 ect. So pick a point on the terrain and move towards it.
So we need the rain to move across the x and z axis only within the parameters of the given input. The rain has to move all as one object which makes it confusing for having random positions for 2 axis'.
Here's what we got so far it doesn't work properly. It just rapidly moves all over the terrain like it's glitching, but it is pick random points. Also it always sets the Y axis to 14 for some reason.
For X it's 0 to 5000 For Z it's 0 to 5000
Covering all possible terrain areas.
var parameter1 = 0;
var parameter2 = 0;
var parameter3 = 0;
var parameter4 = 0;
parameter1 = Time.time;
parameter2 = Time.time;
parameter3 = Time.time;
parameter4 = Time.time;
transform.position = Vector3(Random.Range(parameter1, parameter2), 809.3739,
Random.Range(parameter3, parameter4));
Also it always sets the Y axis to 14 for some reason.
For X it's 0 to 5000 For Z it's 0 to 5000
Answer by aldonaletto · Mar 15, 2012 at 12:15 AM
You can create a random point and move to it at constant speed with MoveTowards; when the point is reached, create a new random point and repeat the loop:
var speed: float = 20.0; // rain absolute speed var xMin: float = 0.0; // set the terrain limits var xMax: float = 5000.0; var zMin: float = 0.0; var zMax: float = 5000.0;
private var point: Vector3;
function Start(){ // force a new random point in the first Update: point = transform.position; }
function Update(){ if (transform.position == point){ // if current random point reached... point.x = Random.Range(xMin, xMax); // draw new x, z coordinates, but point.z = Random.Range(zMin, zMax); // don't change the original y } // anyway, move towards the current random point: transform.position = Vector3.MoveTowards(transform.position, point, speed*Time.deltaTime); } MoveTowards(a, b, dist) returns a point at dist distance from a in direction to b; the point returned is clamped to b, thus the rain will never pass this point.
NOTE: If you don't want to rain out of the terrain, subtract the ellipsoid half-size from the limits: if it's 500x500, set xMin=250, xMax=4750, zMin=250, zMax=4750, for instance.
Thank you for this it does exactly what I need. There is one problem though, it stops after a few position changes. Is there anyway to continually loop this so it never ends?
Weird thing! It should not stop - the rain should keep moving to the current random point, and draw a new one whenever the current point was reached.
You've said the rain was changing the Y coordinate to 14 due to some reason. $$anonymous$$aybe some other script is doing this, what could even prevent the rain to reach the current random point: if some other script is setting y to 14 each update or fixed update, the rain may get stuck at the current x,z position, never reaching the desired y.
You could prevent this forcing y to the correct value:
function Update(){ transform.position.y = point.y; // restore rain y if (transform.position == point){ // if current random point reached... ...
Actually it I'd work, it can't be attached to another object, but you can attach object to it. Thanks again
Answer by DaveA · Mar 15, 2012 at 12:09 AM
I'm guessing this is in an Update function?
parameter1 = Time.time;
parameter2 = Time.time;
parameter3 = Time.time;
parameter4 = Time.time;
That's going to set all 4 parameter variables to the same exact value. So not very random. Why not use something like Random.Range(0f,5000f)? Or Random.value * 5000?
Are you trying to pick random spots for this rain patch to move, and have it move over time? I would look into using Mathf.Lerp (see scripting reference). Pick a new random position every so often (but not every frame!) and set a 'target' position. Then let it Lerp toward that target.
Your answer
Follow this Question
Related Questions
Constant camera shake. 1 Answer
Controlling parent by Inputs 1 Answer
How to change direction without changing speed? 1 Answer
ScreenToWorldPoint fixed height 1 Answer