Skip to content

Instantly share code, notes, and snippets.

@barleymalt
Last active March 13, 2023 13:30
Show Gist options
  • Save barleymalt/c114abf1690775b1c7d8843424d68d92 to your computer and use it in GitHub Desktop.
Save barleymalt/c114abf1690775b1c7d8843424d68d92 to your computer and use it in GitHub Desktop.
public static class Vector3Extensions
{
public static Vector3 GetPointOnSphereAngle(this Vector3 center, Vector3 targetDirection, float angle, float radius)
{
float angleInRad = Random.Range(0.0f, angle) * Mathf.Deg2Rad;
Quaternion dir = Quaternion.LookRotation(targetDirection - center);
Vector2 pointOnCircle = (Random.onUnitSphere) * Mathf.Sin(angleInRad);
Vector3 pointOnSphere = dir * (new Vector3(pointOnCircle.x, pointOnCircle.y, Mathf.Cos(angleInRad))) + center;
Vector3 distFromCenter = (pointOnSphere - center) * radius;
return pointOnSphere + distFromCenter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment