Quantcast
Channel: Answers for "Angle to position on a cube"
Viewing all articles
Browse latest Browse all 4

Answer by aldonaletto

$
0
0
That's a good solution, but will fail when the cube is rotated and/or not centered at (0,0,0). Additionally, each time you read cube.transform.localScale, Unity makes calls to the internal functions *getTransform* and *getLocalScale*, what wastes time.
Your algorithm may be changed to solve all these problems:
  // get a random point p in the surface of the sphere
  // that encapsulates a cube with dimensions 1x1x1:
  Vector3 p = Random.onUnitSphere;
  // flatten the point on the 1x1x1 cube surface:
  p.x = Mathf.Clamp(p.x, -0.5f, 0.5f);
  p.y = Mathf.Clamp(p.y, -0.5f, 0.5f);
  p.z = Mathf.Clamp(p.z, -0.5f, 0.5f);
  // convert the point to the actual cube surface, according
  // to its position, rotation and scale:
  p = cube.transform.TransformPoint(p);
The first four instructions actually create a kind of Random.onUnitCube function, and the last one transform it to the actual cube surface.

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images