Questions on Unity3D , as far as I see quite rare, I am afraid to get a lot of drawbacks, but there is a question.

How to implement the rotation of the camera around the object, but at the same time, that would be when you move the object, the camera went behind the object.

The picture shows a camera and a ball.

enter image description here

There are CameraController and PlayerController.

There is a rotation of the camera.

 transform.RotateAround(playerGO.transform.position, new Vector3(0, 1, 0), 1*Time.deltaTime); 

And the movement of the ball itself

 movement += Input.GetAxis("Horizontal") * cameraCtrl.SideDirection; movement += Input.GetAxis("Vertical") * cameraCtrl.ForwardDirection; rigid.AddForce(movement*speed); movement = Vector3.zero; 

The problem is that when the ball changes position, the camera simply rotates further around the ball, but the ball can go beyond the limits of the camera's visibility and it will rotate further, how to fix it?

  • If the camera is spinning around the ball all the time, and moving the ball too, how can the ball go beyond the limits of the camera's view? Please explain. - zverkov
  • if the ball moves to the camera, then the point around which the camera turns will be where the ball is, but already out of sight, although it will turn around. it's like to be back to the ball, but still spin around - Yaroslav Movchan

2 answers 2

I do it very simply, I place in the object of the ball a nested empty object (observer) removed at a distance where the camera should be. Next, we write a controller that controls the position of the observer relative to the parent object (ball). On the same camera, we hang a script that moves the camera along transform.position.Lerp (for smooth movement) to the observer's point and on each frame we turn the camera towards the observed object (ball). I will publish the code with an example after the holidays, there are ready-made checked versions at work.

  • This method is much easier than what I used. I determined the vector, the short one pointed out the object from the camera (elementary trigonometry) and then already shifted, the distance and angle were twisted, thank you, this is what you need) - Jaroslav Movchan

I'm not at home now - there is no Unity3D environment at hand, so I can only assume a roughly working option (or maybe not working at all: D), but given the fact that Unity3D is rarely answered here, the approximate version is probably better than nothing :)

 public class CameraFollow : MonoBehaviour { public Transform targetTransform; // сюда прокидываете вашего playerGO.transform public float smothing = 5f; Vector3 offsetFromPlayer = new Vector3(1f, 1f, 1f); void Start() { offsetFromPlayer = transform.position - targetTransform.position; } void FixedUpdate() { Vector3 cameraPositionWithOffset = targetTransform.position + offsetFromPlayer; transform.position = Vector3.Lerp(transform.position, cameraPositionWithOffset, smothing * Time.deltaTime); transform.RotateAround(targetTransform.position, new Vector3(0, 1, 0), 10 * Time.deltaTime); } } 

According to the results of testing, write down just so that I know to delve into Unity , when I get home, or you have it working there, or you yourself have found a different solution :)

  • A little bit wrong, but thanks anyway) - Yaroslav Movchan