- Home /
How to rotate the camera around an object
I want the camera to rotate around a ball, while the ball can move up and down and such. So like if the ball were to take a turn, then the camera would turn around with it. My code was: public class CameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
public float rotateSpeed = 3.0F;
void Start()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
transform.position = player.transform.position + offset;
transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
}
}
Answer by Ermiq · Dec 09, 2019 at 09:59 AM
What do you actually want? Do you want camera to just repeat the balls rotations so it will always rotate around the ball while the ball will be rotated around its axes? In this case, if the ball is controlled by physics, it will give you a crazy camera that will spin around the ball while the ball is spinning along the ground/terrain.
Or maybe you control the ball rotation manually and you want the camera to follow the ball rotations?
In this case you'll need to do the following:
You'll need to update the offset
in Update(). Because if you set the offset
just in the Start() method, the vector will be pointing out at the same direction in world coordinates system as it has been pointing at the Start(). And using the same vector as an offset, you actually tell the camera not to turn around the ball.
Also, you need to make the offset
dependent on the ball rotation. So, when the ball rotates, the offset
vector would point out from the ball in some direction relative to the ball current rotation.
For example, you'll need to preset the desired distance between the camera and the ball (e.g., offsetDistance = 2f
) and use the transform's forward
property which represents the transform's current forward direction (the direction the object is looking at).
void Update() {
Vector3 positionForCamera = player.transform.position - player.transform.forward * offsetDistance;
}
This will give you a position that will always stay behind the player/ball's back on the desired distance. And when the player/ball will rotate, its forward
vector will change too, therefore the positionForCamera
will change too, because it depends on the player/ball's forward
direction vector.
UPDATE (forgot about rotation):
And then you need to rotate the camera to look at the ball:
void LateUpdate() {
//set camera position
transform.position = positionForCamera;
//set camera rotation
transform.rotation = Quaternion.LookRotation(player.transform.position - transform.position, Vector3.up);
}
@ermiq I did exactly what you said, but the positionForCamera is never 'resolved', and never seems to see that it is made in Update(). public class CameraController : $$anonymous$$onoBehaviour {
public GameObject player;
private Vector3 offset;
public float offsetDistance = 2f;
public float rotateSpeed = 3.0F;
void Start()
{
offset = transform.position - player.transform.position;
}
void Update()
{
Vector3 positionForCamera = player.transform.position - player.transform.forward * offsetDistance;
}
void LateUpdate ()
{
//set camera position
transform.position = positionForCamera;
//set camera rotation
transform.rotation = Quaternion.LookRotation(player.transform.position - transform.position, Vector3.up);
// transform.position = player.transform.position + offset;
// transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
}
}
I also keep getting the error: UnassignedReferenceException: The variable of CameraController has not been assigned. --It is talking about the positionForCamera
$$anonymous$$y code was a pseudo-code that should have been adjusted a bit to actually work in your setup.
Particularly, you should have made positionForCamera
a global variable to use it in both Update() and LateUpdate(). If you have done that, you wouldn't get the unassigned
exception.
So, put the declaration of the positionForCamera
outside of methods to make it assessible from any method within the class:
Vector3 positionForCamera;
void Update() {
positionForCamera = player.transform.position - player.transform.forward * offsetDistance;
}
Unity doesn't change the camera position in your current code exactly because of the error. LateUpdate() doesn't know what is the positionForCamera
and it doen't assing the camera position to this variable because it's incapsulated within the Update() and is available only within the Update(). To make the LateUpdate() know about this variable you need to make it global (declared somewhere outside of method scopes).
Answer by $$anonymous$$ · Dec 09, 2019 at 12:37 PM
if you want camera to follow ball just make the camera child of ball gameobject and it will move and rotate with it.
Your answer
Follow this Question
Related Questions
Moving and rotating a ball 0 Answers
Rotate a moving object 0 Answers
Wrong rotation while moving 1 Answer