I’m trying to configure the camera to follow my player, but I’m having trouble limiting the rotation of the x angle as I don’t want the camera above the head or under the feet.
This is the code so far:
using System;
using Unity.Mathematics;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
public Vector3 offset;
[Range(0.01f, 1.0f)]
public float smoothing = 0.125f;
public bool Look = false;
public float speed = 2.0f;
private float rotY, rotX;
void LateUpdate()
{
if (target)
{
Vector3 desiredPosition = target.position + offset;
transform.position = Vector3.Slerp(transform.position, desiredPosition, smoothing);
rotY = Input.GetAxis("Horizontal") * speed;
rotX = Input.GetAxis("Vertical") * speed;
//Limit the X rotation of the camera
rotX = Mathf.Clamp(rotX, -10, 30);
//Rotate camera
offset = Quaternion.AngleAxis(rotY, Vector3.down) * offset;
offset = Quaternion.AngleAxis(rotX, Vector3.right) * offset;
if (Look)
{
transform.LookAt(target);
}
}
}
}