I am trying to get a random world position inside my targets collider, so that I can fire an arrow at it.
I have tried a bunch of different solution but can't seem to get it right, for example (this code is on my target):
//Inside Awake
colliders = GetComponentsInChildren<Collider>(); //I want this to work on a unit with a single capsule collider and a building with 8 different box colliders
public Vector3 GetRandomPoint()
{
Bounds b = new Bounds();
for (int i = 0; i < colliders.Length; i++)
{
b.Encapsulate(colliders[i].bounds);
}
float minX = gameObject.transform.position.x - gameObject.transform.localScale.x * bounds.size.x * 0.5f;
float minZ = gameObject.transform.position.z - gameObject.transform.localScale.z * bounds.size.z * 0.5f;
return new Vector3(Random.Range (minX, -minX),
gameObject.transform.position.y,
Random.Range (minZ, -minZ));
}
In the scenario I'm having issues with right now, the trageet simply has a single capsule collider.
This return a point quite far away from my target, but I cant find the issue.
EDIT:
Here is another one I tried without success:
public Vector3 GetRandomPoint()
{
Bounds b = new Bounds();
for (int i = 0; i < colliders.Length; i++)
{
b.Encapsulate(colliders[i].bounds);
}
var target = new Vector3(
UnityEngine.Random.Range(b.min.x, b.max.x),
UnityEngine.Random.Range(b.min.y, b.max.y),
UnityEngine.Random.Range(b.min.z, b.max.z)
);
return b.ClosestPoint(target);
}
new Bounds
gives you a bounds that contains the world origin. Do you really want to encapsulate the world origin when your colliders are a long distance away from it? \$\endgroup\$ – DMGregory♦ Sep 12 '20 at 12:55new bounds
? If a building has 8 different box colliders, I want to generate a random point inside any of these. \$\endgroup\$ – Majs Sep 12 '20 at 12:56