- Home /
How do you move a camera in a circle around a gameobject?
Hello! I'm working on a game where the camera follows the player and orbits around it's position. I've been using the RotateAround function to have it orbit around the player in one direction using a mouse. Since it is only meant to orbit around as the player moves further or closer to the camera the perimeter of the circle it orbits around will shrink and expand accordingly. Is there an easy way to have it stay at a fixed position from the player or do I need to find a different way to approach things? The code I'm working with is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public GameObject player;
public GameManager gameManager;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = player.transform.position + transform.position;
transform.LookAt(player.transform.position);
transform.RotateAround(player.transform.position, Vector3.up, Input.GetAxis("Mouse X") * gameManager.mouseSensitivity * Time.deltaTime + 0.0000001f);
}
}
If it helps to visualize what I'm thinking I made a representation of my idea: Google Drive Image (.jpg)
Thank You!
Answer by Optrix · Jul 04, 2019 at 12:34 AM
I create an empty GameObject (called CameraBase) and place the camera in it. The local position of the camera should NOT be zero. I often start out at 0,0,-10.
Place the CameraBase under the player object in the scene hierarchy.
Make sure that the camera is turned to look towards the player.
Now you've got a camera that follows the player wherever they go. As a bonus, you can rotate the camera in a circle around the player just by adjusting the transform.localRotation of the CameraBase.
You can 'zoom in' by changing the local position of the camera - if it starts at (0,0,-10), you can zoom in on the player by moving it to (0,0,-5), and zoom out by moivng it to (0,0,-20).
If you find the camera movement a little too jerky or abrupt, you can make the CameraBase smoothly follow the player instead of being an actual child of it. This can fix some user discomfort when your player is moving extremely quicky (involved in collisions etc.).
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Translating Speed into rotation degress\sec 1 Answer
Distribute terrain in zones 3 Answers
Orbit controlled by virtual joystick 1 Answer
Orbit Camera around object 2 Answers