Follow along with my tutorial for Unity as I show you the best way to create a third-person camera system using C#.
If you want to copy the code snippet and follow along with the video feel free to do so below. It’s written in C#. Just create 2 new C# files one called “CameraCollision” and the other “CameraFollow” and paste the code inside it.
CameraCollision C# Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraCollision : MonoBehaviour {
public float minDistance = 1.0f;
public float maxDistance = 4.0f;
public float smooth = 10.0f;
Vector3 dollyDir;
public Vector3 dollyDirAdjusted;
public float distance;
// Use this for initialization
void Awake () {
dollyDir = transform.localPosition.normalized;
distance = transform.localPosition.magnitude;
}
// Update is called once per frame
void Update () {
Vector3 desiredCameraPos = transform.parent.TransformPoint (dollyDir * maxDistance);
RaycastHit hit;
if (Physics.Linecast (transform.parent.position, desiredCameraPos, out hit)) {
distance = Mathf.Clamp ((hit.distance * 0.87f), minDistance, maxDistance);
} else {
distance = maxDistance;
}
transform.localPosition = Vector3.Lerp (transform.localPosition, dollyDir * distance, Time.deltaTime * smooth);
}
}
CameraFollow C# Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour {
public float CameraMoveSpeed = 120.0f;
public GameObject CameraFollowObj;
Vector3 FollowPOS;
public float clampAngle = 80.0f;
public float inputSensitivity = 150.0f;
public GameObject CameraObj;
public GameObject PlayerObj;
public float camDistanceXToPlayer;
public float camDistanceYToPlayer;
public float camDistanceZToPlayer;
public float mouseX;
public float mouseY;
public float finalInputX;
public float finalInputZ;
public float smoothX;
public float smoothY;
private float rotY = 0.0f;
private float rotX = 0.0f;
// Use this for initialization
void Start () {
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update () {
// We setup the rotation of the sticks here
float inputX = Input.GetAxis ("RightStickHorizontal");
float inputZ = Input.GetAxis ("RightStickVertical");
mouseX = Input.GetAxis ("Mouse X");
mouseY = Input.GetAxis ("Mouse Y");
finalInputX = inputX + mouseX;
finalInputZ = inputZ + mouseY;
rotY += finalInputX * inputSensitivity * Time.deltaTime;
rotX += finalInputZ * inputSensitivity * Time.deltaTime;
rotX = Mathf.Clamp (rotX, -clampAngle, clampAngle);
Quaternion localRotation = Quaternion.Euler (rotX, rotY, 0.0f);
transform.rotation = localRotation;
}
void LateUpdate () {
CameraUpdater ();
}
void CameraUpdater() {
// set the target object to follow
Transform target = CameraFollowObj.transform;
//move towards the game object that is the target
float step = CameraMoveSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards (transform.position, target.position, step);
}
}