- Home /
Distributing agents over an area
Hello,
I have multiple agents (hundreds) in my scene, heading to different areas. An area is a GameObject with a cube and a mesh renderer, material is translucent.
At start each agent have one (random choice) of the area.transform.position as it's destination. Once it gets closer to the area the agent will (should) go somewhere random in the area. It was working properly with axis aligned areas but wasn't working when the area was rotated on Y. I did as it follows : (GoalID is a var where I put the gameObject with the area the agent is going to)
if(!DestinationReached)
{
if((this.transform.position - GoalID.transform.position).sqrMagnitude < 15*15 && GoalID.tag == "Goal")
{
GetComponent(NavMeshAgent).destination = GoalID.transform.TransformPoint(Vector3(Random.Range(0,GoalID.transform.localScale.x),0,Random.Range(0,GoalID.transform.localScale.z)) - GoalID.transform.localScale/2);
Debug.Log(GetComponent(NavMeshAgent).destination); //it works, so this part of code is achieved.
DestinationReached = true; //Was false before, then get true sp this part of the code only happens once.
}
}
When i do so, all my agents are heading to the same point. Before i was using this line :
GetComponent(NavMeshAgent).destination = (Vector3(GoalID.transform.position.x + (Random.Range(-GoalID.transform.localScale.x / 2, GoalID.transform.localScale.x / 2)), GoalID.transform.position.y, GoalID.transform.position.z + (Random.Range(-GoalID.transform.localScale.z / 2, GoalID.transform.localScale.z / 2))));
But as i said, it wasn't working with rotated areas, my destination was sometime pointing outside the area... If you have an idea, i'll be happy to try and to understand where i am wrong.
Thank you very much. Fabien.
Answer by FabienTT · Mar 09, 2013 at 03:23 PM
Finally :
GetComponent(NavMeshAgent).destination = GoalID.transform.TransformPoint(Vector3(Random.Range(-0.5f, 0.5f),0,Random.Range(-0.5f, 0.5f)));
Did the job perfectly. I was changing the scale but since TransformPoint is doing it, it wasn't necessary. Thank you for your help.
Answer by MountDoomTeam · Mar 09, 2013 at 09:12 AM
so you need to find a formula to mark the bounds of the area if it is rotated on the y-axis.
on this page is the formula for a box
http://www.econym.demon.co.uk/isotut/simple.htm
You can use it for a rectangle as well, and square, it's basically the same without y-axis and you add a X/Z multiplier for rectangles.
when you have a form of the square, you can say a condition so that the agent only stays in areas of the square equals smaller than one or something like that. Update the function that tells the agent where in the square he is.
Normally it's a bit easier using distance than squares, and invisible Colliders, just make around areas, and oblige the agency stay in the area using distance.
you can make an invisible Collider that only the agent bumps into. Using some kind of selective Collider thing
Not sure if I understood, actually I want to get a random position inside a rectangle area (on x and z axis) even if the rectangle is being rotated on y-axis. As you stated in your first sentence. i don't see how i could use the rectangle formula in your link to handle the rotation of my rectangle.
Am sorry am not very good in math and geometry, anyway, thank you for your help.
Your answer
Follow this Question
Related Questions
Prefabs not finding MonoBehaviours moved into a plugin DLL 1 Answer
Distributing and Updating our Unity MMO causes 'Failed to load PlayerSettings' error. 0 Answers
creating active objects or spots otherwise said HotSpots 1 Answer
Application.LoadLevel When inside a area. (Only script) 1 Answer
Microphone Area Reverb 0 Answers