New to Unity - Need help with 3rd person camera!!

So like I said, I’m new to Unity… 2 days to be exact. I’m working on building a camera and haven’t been able to find what I need. Here’s the code for my character:

var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var xSpeed = 250.0;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
private var x = 270.0;

function FixedUpdate() {
	if (grounded) {
		// We are grounded, so recalculate movedirection directly from axes
		moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		
		if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
		}
		else if (Input.GetKey("a")) { //rotate person
			x -= .28 * xSpeed * 0.02;
			transform.localRotation = Quaternion.AngleAxis (x, Vector3.up);
		}
		else if (Input.GetKey("d")) { //rotate person
			x += .28 * xSpeed * 0.02;
			transform.localRotation = Quaternion.AngleAxis (x, Vector3.up);
		}
	}
	if (Input.GetAxis("Vertical"))
		animation.CrossFade("Run");
	else
		animation.CrossFade("Idle");
	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(moveDirection * Time.deltaTime);
	grounded = (flags  CollisionFlags.CollidedBelow) != 0;
}

@script RequireComponent(CharacterController)

And here is the code for my camera, which is part of my character:

var target : Transform;
var distance = 7.0;

var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

private var x = 0.0;
private var y = 0.0;

private var origx = -90;

@script AddComponentMenu("Camera-Control/Mouse Orbit")

function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

	// Make the rigid body not change rotation
   	if (rigidbody)
		rigidbody.freezeRotation = true;
}

function LateUpdate () {
    if (Input.GetKey("a")) { //rotate camera
		//origx = x -= .28 * xSpeed * 0.02;
		//transform.LookAt(target);
	}
	else if (Input.GetKey("d")) { //rotate camera
		//origx = x += .65 * xSpeed * 0.02;
	}
	else {
		if (target  Input.GetMouseButton(0)) { //if mouse clicked, rotate camera
			x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
			y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
			y = ClampAngle(y, yMinLimit, yMaxLimit);
		}
		
		else if (Input.GetKeyUp("f10")) { //switch between 1st and 3rd person views
			if (distance > 0) {
				distance = 0;
				y = 0;
			}
			else {
				distance = 7;
				y = 45;
			}
		}
		else if (Input.GetMouseButtonUp(0)) { //reset camera to original spot
			x = origx;
			if (distance == 0)
				y = 0;
			else
				y = 45;
		}
		else if (Input.GetAxis("Mouse ScrollWheel") < 0) { //zoom out
			if (distance < 12  distance != 0)
				distance += .5;
		}
		else if (Input.GetAxis("Mouse ScrollWheel") > 0) { //zoom in
			if (distance > 4)
				distance -= .5;
		}
		
		if (distance > 0)
			Calculate3rd();
		else
			Calculate1st();
	}
}

function Calculate3rd() { //recalculates angle of camera
	var rotation = Quaternion.Euler(y, x, 0);
	var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    transform.rotation = rotation;
    transform.position = position;
}

function Calculate1st() {
	var rotation = Quaternion.Euler(y, x, 0);
	var position = rotation * Vector3(0.0, 1.5, .5) + target.position;
	transform.rotation = rotation;
	transform.position = position;
}

static function ClampAngle (angle : float, min : float, max : float) {
	if (angle < -360)
		angle += 360;
	if (angle > 360)
		angle -= 360;
	return Mathf.Clamp (angle, min, max);
}

So what I’m having an issue with is my character rotation. When I hit ‘a’ or ‘d’, the character should rotate and the camera should stay center to the back of the character. The character does rotate, but the camera angle is always off. I wasn’t sure if I needed my camera to rotate or look at the target, so I commented out that code, and it works until I let go. Then it resets to the original position. Any help on what I need to do?

Welcome to the forum!

Apologies if I’m missing something here, but if you want the camera to stay exactly behind the character, then couldn’t you just make it a child of the character?

I assume this is what you mean?

208985--7682--$untitled_172.png

Yes. Does it do what you want?

I am struggling with the same issue (also a beginner).

i guess that your camera is also rotating together with your character but you want the camera to stay normal instead of rotating with the character.

Someone told me you can unlink the main camera so don’t use him as a child of your character but then you will have to write a script so the camera will follow the character.

I am planning on looking into this in the evening so i will be posting back if i get my issue resolved :slight_smile:

No it’s not doing what I want. I guess I should explain better.

The scripts I have written right now has the camera following the character. If you pressed the mouse down, you can rotate the camera as you run to see what’s behind you or if your standing still and just want to look at you character. If you press F10, it will go between a 1st and 3rd person view with that same camera rotation function. Also, the mouse scroll allows a zoom in/out function in 3rd person.

The issue I’m having is as follows: When you press ‘a’ or ‘d’ the character should rotate which allows the person to turn as he moves. The problem is I have to hard code the speed that the camera should be rotating at, and it differs depending on the size of the screen. While ‘a’ or ‘d’ is being pressed, I’d like the camera to stay behind the character, like it would if I was running normally. I just can’t find the best way to do that…

Also, the way the code is set up right now, I commented out my code for when ‘a’ and ‘d’ is pressed and the camera follows it perfectly. But when I let go, the camera sets back to where it originally was, and I’m not sure how to make it stay where it is.