# Camera rotation around player while following.

I’m trying to achieve a camera that follows and rotates around the player while allowing the player to do things like rotate in C#. This means that the player and camera need to rotate separately, but I find it impossible to get the camera to rotate if I set it’s position to follow the player. I know exactly what the problem with my code is, but I have no idea how to fix it! Please help!

``````    public float turnSpeed = 0.40f;
public Transform player;

void Update()
{
transform.position = new Vector3(player.position.x, player.position.y + 8.0f, player.position.z + 7.0f);

transform.LookAt(player.position);

transform.RotateAround(player.transform.position, Vector3.up, Input.GetAxis("Mouse X") * turnSpeed);
}
``````

Here are a couple of changes that give what I think you want:

``````using UnityEngine;
using System.Collections;

public class Orbit : MonoBehaviour {

public float turnSpeed = 4.0f;
public Transform player;

private Vector3 offset;

void Start () {
offset = new Vector3(player.position.x, player.position.y + 8.0f, player.position.z + 7.0f);
}

void LateUpdate()
{
offset = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
transform.position = player.position + offset;
transform.LookAt(player.position);
}
}
``````

HO HOHOHO Here’s how to get the camera to rotate around the X axis or rather over and under the player.

``````using UnityEngine;
using System.Collections;

public class Orbit : MonoBehaviour {

public float turnSpeed = 4.0f;
public Transform player;

public float height = 1f;
public float distance = 2f;

private Vector3 offsetX;
private Vector3 offsetY;

void Start () {

offsetX = new Vector3 (0, height, distance);
offsetY = new Vector3 (0, 0, distance);
}

void LateUpdate()
{
offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, Vector3.up) * offsetX;
offsetY = Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offsetY;
transform.position = player.position + offsetX;
transform.LookAt(player.position);
}
}
``````

i used it too you need this modification of that

public float turnSpeed = 10f;
public Transform player;

``````public float height = 1f;
public float distance = 2f;

private Vector3 offsetX;
private Vector3 offsetY;

void Start () {

offsetX = new Vector3 (0, height, distance);
offsetY = new Vector3 (0, 0, distance);
}

void LateUpdate()
{
offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offsetX;
offsetY = Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offsetY;
transform.position = player.position + offsetX + offsetY;
transform.LookAt(player.position);
}
``````

I tried your suggestion but edited to fit my needs, rotates camera around public Transform variable “player”, wherever that player object may be.
This only rotates the camera around the x axis of the player’s position. How can I raise the camera to slightly above the player’s position?
Any suggestions on rotation on the Y axis?

``````using UnityEngine;
using System.Collections;

public class Orbit : MonoBehaviour {

public float turnSpeed = 4.0f;
public Transform player;

public float height = 1f;
public float distance = 2f;

private Vector3 offsetX;

void Start () {

offsetX = new Vector3(0, height, distance);
}

void LateUpdate()
{
offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offsetX;
transform.position = player.position + offsetX;
transform.LookAt(player.position);
}
}
``````

@Omega MrBalins code is perfect he just left out part of line 25 so ppl couldn’t copy and paste his work

I’m having similar issues, I can implement the camera controls so that it follows the player object, Except when I try to add X axis rotation it all goes tit’s up I know this is old, but I ran into the same issue, and I read through everything and copied MrBalins code, with the fix from a few comments down, and the camera would still go inside the player object as a child as a GameObject, with the camera as a child object, then i finally figured out i could just multiply the x axis to stop it from going inside the object when you turn around the object close to the ground. Im going to be adding a condition to keep the camera from being able to go under the ground but heres the code for anyone who needs it in the future.

``````public Transform player;

public float turnSpeed = 4.0f;
private float height = 5f;
private float distance = 7f;

private Vector3 offsetX;
private Vector3 offsetY;

void Start ()
{
offsetX = new Vector3 (0, height, distance * 4);
offsetY = new Vector3 (0, 0, distance);
}

void LateUpdate()
{
offsetX = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offsetX;
offsetY = Quaternion.AngleAxis (Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offsetY;
transform.position = player.position + offsetX + offsetY;
transform.LookAt(player.position);
}
``````

in the Start where offsetX is multiplied by 4 is what stops the camera from going in the object. The orbit is offset from the player so the camera on the close orbit goes inside the player. this stops that. 2f would work as well but i wanted a bit more distance.

here is a simple change that gave me a nice y axis rotation with simple x axis for camera
for some reason late update doesn’t work for me has to use fixed update.

``````public Transform player;        //Public variable to store a reference to the player game object
public float turnSpeed = 4.0f;

private float isDragging = 0.0f;

public Vector3 offset;
void FixedUpdate ()
{
if (Input.GetMouseButton(0)){
if(isDragging > 0.15f){
offset = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset;
offset.y -= Input.GetAxis("Mouse Y") * turnSpeed;
offset.y = Mathf.Clamp(offset.y,10,25);
}
isDragging += Time.deltaTime;
//Debug.Log(isDragging);
}
else{
isDragging = 0;
}
transform.position = Vector3.Lerp(transform.position, player.position + offset,0.1f);
transform.LookAt(player.position);
}
``````