Home > Enterprise >  Unity3d C# function to place cameras at edge of hemisphere with the correct direction
Unity3d C# function to place cameras at edge of hemisphere with the correct direction

Time:08-11

I have a hemisphere with radius 250. Located on the edge of the hemisphere (15 degrees above the base/horizon), I have 7 equally spaced points (i.e., 1 point every 360/7 degrees).

Using Unity3D, I want to place camera at each of those 7 points such that their FoV is perpendicular/orthogonal to the surface of the sphere.

Given camera number (1,2,3...7) and the radius of the hemisphere (250), what function will place the cameras in the correct position and facing the correct direction?

CodePudding user response:

pointCount = 7;
tilt = 15;
radius = 250;

void PositionCamera(int pointCount, int pointId, float radius, float tilt, Transform camera){
  Vector3 temp = Vector3.forward * radius;
  temp = Quaternion.Euler(tilt, 0, 0) * temp;
  temp = Quaternion.Euler(0, 360/pointCount*pointId, 0) * temp;
  camera.position = temp;
  camera.rotation = Quaternion.LookRotation(temp);
}

Assuming that the center of the hemisphere is at origin and the first point on the z axis.

  • Related