Skip to content

Commit c87a24c

Browse files
committed
Add Bounds and Collider extensions to generate random points
1 parent f4ac1d9 commit c87a24c

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.Math
4+
{
5+
public static class BoundsExtensions
6+
{
7+
public static Vector3 RandomPointInside(this Bounds bounds)
8+
{
9+
return new Vector3(
10+
Random.Range(bounds.min.x, bounds.max.x),
11+
Random.Range(bounds.min.y, bounds.max.y),
12+
Random.Range(bounds.min.z, bounds.max.z));
13+
}
14+
15+
}
16+
17+
}

Runtime/Extensions/BoundsExtensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.Math
4+
{
5+
public static class ColliderExtensions
6+
{
7+
public static Vector3 RandomPointInside(this Collider collider)
8+
{
9+
Vector3 randomPoint = collider.bounds.RandomPointInside();
10+
Vector3 closetPoint = collider.ClosestPoint(randomPoint);
11+
12+
if (closetPoint == randomPoint) { // inside
13+
return randomPoint;
14+
} else { // outside
15+
return Vector3.Lerp(closetPoint, collider.bounds.center, Random.value);
16+
}
17+
}
18+
19+
public static Vector3 RandomPointInside(this Collider2D collider)
20+
{
21+
Vector3 randomPoint = collider.bounds.RandomPointInside();
22+
Vector3 closetPoint = collider.ClosestPoint(randomPoint);
23+
24+
if (closetPoint == randomPoint) { // inside
25+
return randomPoint;
26+
} else { // outside
27+
return Vector3.Lerp(closetPoint, collider.bounds.center, Random.value);
28+
}
29+
}
30+
31+
}
32+
33+
}

Runtime/Extensions/ColliderExtensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)