- Home /
Random position inside multiple areas
Hi,
I got an object following a Waypoint in 3d space. Every time the player collides with the waypoint, the waypoint moves(teleports) to another location within given range. This is the script im using for the waypoint;
function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == "Sphere"){
this.transform.position = Vector3(Random.Range(-80, 100), Random.Range(5, 10), Random.Range(-80, 100));
}
}
This works fine but i would like to limit the randomness within two different areas. (See image - pink dot is wayponit). If the waypoint is inside Area#1 when it collides with the object it will move to a random position inside Area#2. Then, when it collides with the object inside Area#2 it will move to a random position inside Area#1 and so on.
I would be most grateful for any ideas, help, advice on this!
/Fredrik
Answer by sparkzbarca · Mar 11, 2013 at 12:00 PM
This should be pretty easy
Take and make 2(or infinite) empty game objects which define the (0,0,0) point of a possible AREA.
This allows you to move within a range but to have that range start at that point. Your going to move in the local space of that object (that object will be the center of the universe basically)
To simplify things for later you should probably take and gather all these objects up and make a list of gameobjects
first we'll pick a random area to go to EXCEPT we wont go to the same area we are currently in. You can ignore this step if you want to go in a sequential order. from area 1 to 2 and so on. In that case just iterate through the list instead of picking a random one.
bool FoundNewArea = false;
gameobject Area;
int iterator;
int randomIterator;
while(!FoundNewArea)
{
RandomIterator = random.range(0,ListOfGameobjects.size -1);
//this next line makes sure we didnt pick to go to where we already are,
//again you can remove this if you want a CHANCE you pick the area your already in
if(iterator != RandomIterator)
{
iterator = randomiterator
FoundNewArea = true;
}
}
transform.position = ListOfGameObjects[iterator].transform.position + randomVector;
Of course now each area would be uniform in size you could fix that too but this code would do what you want which is to take whatever area your in. Find a new area out of a list of possible ones. And move to a random location int aht area.
basically make new areas defined by empty game objects. Collect them in a list and
transform.position = ListOfGameObjects[iterator].transform.position + randomVector;
after that you just decide if picking the new area is random or sequential and if the areas are random in size or uniform and if you can pick the area your in at the moment or not etc
Answer by fiorentin · Mar 11, 2013 at 01:05 PM
Thank you for your answer! I will have to look into making lists. I really don't know much about code at all but i guess your code is in c#. Can I translate and implement this in my code above so that the action happens when colliding? I guess translating my short one would be easier.
Hmm nope cant seem to implement this with the collide code. Any suggestions out there? Would be most appreciated!
Your answer
Follow this Question
Related Questions
Spawn GameObjects without overlap 1 Answer
Random Running and Follow at Certain Distance 0 Answers
Random Position For GameObject 1 Answer
[Beginner] Using Random.Range in initial game object position 1 Answer
AI random target 2 Answers