Hello everybody; I hope this has not been answered before, I spent all of yesterday searching the internet but couldn’t find an answer.
The situation is: I have a sphere (an IcoSphere actually) and I have a mesh which moves on the surface of the sphere, controlled by the player. The way I am doing this is to have a script which creates a parent object to the mesh; the parent object is located in the center of the sphere so player movement is achieved by rotating the parent object. Code follows for the player controller:
I would like to move the camera so that it always follows the player from behind. I can do this by making the camera a child of the player object, but since I would like to do some Lerp magic and make the camera chase with a little delay, I would like to have more control over its position as making it a child makes the position update instantaneous.
How can I have the camera position and rotation be updated so that it is always behind the player?
You have to keep in mind that when your camera is a child of the player, then all movement of its parent (the player) will affect the camera’s position also.
What I recommand:
Use Cinemachine. Makes your life pretty easy. And has all the features you need (A lot of Videos and Docs about CInemachine exists).
Alternatively if you want to make it without Cinemachine:
→ Make the camera not a child of the player
→ or use some kind of empty non-moving wrapper object for the Player and camera like:
Scene
----Wrapper
---------Player
---------Camera
→ or it gets more complicated if you want to do it as child. It is not impossible but quite annoying coding
Probably there are more ways to achieve this. The simplest (and best imo), is to just use Cinemachine or alternatively make the camera not a child of the Player object
Thank you for your answer, I will check cinemachine out! I do not want the camera to be a child of the player, I’d like it not to be indeed, but I am struggling with the math to keep the camera behind the player and looking at it as the player revolves around the center of the sphere.
Thank you, I kinda of solved the problem with Cinemachine using a virtual camera with “same as follow target” as Aim property, because both the “composer” Aim property and your script have the problem that the camera turns upside down as the player revolves around the sphere; basically I’d like the camera “down” to be always pointing to the center of the sphere as it follows the player.
The flip happends for the rotation follow on the X axis. LookAat(Transform) takes a “short cut” …
With Cinemachine, you can solve the problem in the vCam properties.
Set the Body to Transposer & the Binding Mode to Lock To Target.
For the script i gave ( im not fighting with Cinemachine, its a wonderfull tool, but just to be complete… And its not MY script but i use it with many variations… ) this can be fixed by adding the Vector3 WorldUp parameter to LookAt()