- Home /
How to adjust camera position/rotation when player looks up or down? (Third Person Camera)
I have a third person camera for the player character in my game. As of now, when the player looks up or down the camera does not change position, but it rotates to keep the player's crosshair in the center of the screen. I would like to have some code in the script for the camera so that when the player looks up or down the camera moves in response. For example, if the player looks down the camera would move up the greater the player looks down. The same can be said for the player looking up, the camera would move down. I would still like to have the crosshairs in the center of the screen. I have tried to do this on my own, but just recently started scripting in unity and working in 3d space for the first time in a while. I also tried using a cinemachine virtual camera but did not have much luck. Any help would be greatly appreciated. I am happy to provide more information and detail if needed or requested.
Answer by Greywolf6 · May 10 at 09:59 PM
Look at Sebastian Graves 3rd person Horror Survival tutorials. It has some good information on how to build a camera system like this. I had this code from another project that you might be able to change up to get where you want to be.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SmoothFollowCamera : MonoBehaviour
{
// The target we are following
public Transform target;
// The distance in the x-z plane to the target
public float distance = 10.0f;
// the height we want the camera to be above the target
public float height = 5.0f;
// How much we
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;
// Place the script in the Camera-Control group in the component menu
[AddComponentMenu("Camera-Control/Smooth Follow")]
void LateUpdate()
{
// Early out if we don't have a target
if (!target) return;
// Calculate the current rotation angles
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;
float currentRotationAngle = transform.eulerAngles.y;
float currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
// Always look at the target
transform.LookAt(target);
}
}
Thanks, I greatly appreciate the answer. I will check out the tutorials you had mentioned and I will play around with the script you provided.