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.
↧