Follow Player which moves over a sphere

After hours of googling and pulling several hairs out, I decided to open this thread.

I want to make a game, in which the player walks around a sphere (like super Mario galaxy) and the camera should stay fixed over the player and point down on its head but I can't seem to get anywhere near of the right method. So, how can I realise this?

Thanks.

• You mean "down" as in "over his head with no angle"? – ibrahim mahrir Jul 17 '18 at 1:07
• @ibrahim I mean down as the player seems fixed and the world looks like its moving instead – prismflux Jul 17 '18 at 1:17
• @user3597374 If you already tried my code I urge you to try it again as I have forgotten a minor detail that I have added now. Sorry! – ibrahim mahrir Jul 17 '18 at 1:31

I think that is a fairly simple thing to do. Just get the vector between the sphere center and the player position, let's call it v. Normalize v and multiply it by the distance you want and then add the result back to v. v is now the distance from the center of the sphere to where the camera shoud be:

Vector3 v = playerTransform.position - sphereCenter;
v += v.normalized * distance;

cameraTransform.position = sphereCenter + v;
cameraTransform.LookAt(playerTransform, cameraTransform.up);


Note: distance should be a number. It represents the distance you wish to have between the player and the camera.

• It might be worth clarifying here that the distance variable is the distance from the player to the camera, not the distance from the center of the sphere to the camera. Also, you may want to manually provide an up vector for the LookAt method, as the default value will cause the view to pivot sharply when the player moves close to the poles of the sphere. – DMGregory Jul 17 '18 at 1:44
• @DMGregory I've added a note concerning that distance variable. About that up vector you mentioned, I don't even know what does that mean as my knowledge in unity is very basic. Feel free to edit the answer! – ibrahim mahrir Jul 17 '18 at 2:05
• The up vector controls how the camera should twist about its viewing axis. By default, it will try to make the world up vector (0, 1, 0) point roughly upward on the screen. But there are two points where it can't do that: when it's looking straight up or straight down. These form singularities in the mapping from positions on the sphere to camera orientations. According to the Hairy Ball Theorem, we'll always get a singularity somewhere if we try to have one globally consistent mapping. Instead, we can use the previous camera's up vector to make a locally consistent mapping. – DMGregory Jul 17 '18 at 2:09
• @DMGregory LOL. chinese, chinese, chinese. I didn't understand a thing. But thanks anyways. I'll dive deep into that when I'm ready to learn unity. Thanks again for the edit. – ibrahim mahrir Jul 17 '18 at 2:16
• Thanks for the code! But unfortunately it doesn't work, because Unity says you cannot use the + operator with objects of type Transform (I replaced cameraTransform with only transform. (Same with playerTransform) – prismflux Jul 17 '18 at 16:02