Help with a simple 3rd person camera?

I’m trying to get a simple 3rd person camera to work. All I want it to do for now is follow my player (a non rigidbody capsule) with the ability to rotate the camera in any direction depending on the mouse movements. Most tutorials I’ve gone through are advanced cameras which I don’t want to dive into yet.

Here’s my current attempt:

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

public GameObject target;
public float rotateSpeed = 5;
Vector3 offset;

void Start() {
	offset = target.transform.position - transform.position;
}

void LateUpdate() {

	float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
	float vertical = Input.GetAxis ("Mouse Y") * rotateSpeed;
	target.transform.Rotate(vertical, horizontal, 0);
	
	float desiredAngle = target.transform.eulerAngles.y;
	Quaternion rotation = Quaternion.Euler(0, desiredAngle, 0);
	transform.position = target.transform.position - (rotation * offset);;
	
	transform.LookAt(target.transform);

}
}

The mouse movements are rotating the player instead of the camera though.

You are applying the rotation to the target rather than the camera itself, which would be why your player is rotating and not the camera.

I suspect you are wanting the camera to rotate around the player? rather than be a freelook camera? If so then use the Transform.RotateAround method (Unity - Scripting API: Transform.RotateAround) to have your camera rotate about the player, but apply the result to the camera’s transform and not the target.

Rather than have:
target.transform.Rotate(vertical, horizontal,0);

Do something like:
transform.RotateAround(target.transform, Vector.Up,horizontal * 10);

and the camera should rotate around your player.

HTH and I haven’t fat fingered it too much :slight_smile: