Cinemachine works great. Until it doesn't. If you have a camera design that requires immediate movement or damped movement depending on mouse button state (like many MMOs do) then you are in for a headache. Cinemachine doesn't like it when the dampen values change while it is currently in a dampening state. No fix to this issue that I've found.
Resources:
docs.unity3d.com/ScriptReference/Mathf.Atan2.html
docs.unity3d.com/ScriptReference/Physics.Raycast.html
docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html
docs.unity3d.com/ScriptReference/Mathf.SmoothDampAngle.html
I just love tutorials when they say "Copy these variables" is all i need
One word: "Cinemachine"
the end.
Cinemachine works great. Until it doesn't. If you have a camera design that requires immediate movement or damped movement depending on mouse button state (like many MMOs do) then you are in for a headache. Cinemachine doesn't like it when the dampen values change while it is currently in a dampening state. No fix to this issue that I've found.
Nice work bro ♥️ waiting for the next parts
The whole script for anyone lazy like me:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour
{
public LayerMask camRaycastMask;
public CharacterController controller;
public Transform cameraTransform;
public Transform cameraPivot;
public float controllerLookSmoothTime;
public float lookSmoothTime;
public float cameraDistance;
public float sensitivity;
public Vector3 pivotOffset;
public Vector2 xRotationLimits;
private float yVelocity;
private float yAngleOffset;
private float currentControllerYRotation;
private float targetControllerYRotation;
private float controllerYRotationVelocity;
private float currentCameraXRotation;
private float currentCameraYRotation;
private float xRotationVelocity;
private float yRotationVelocity;
private float targetXRotation;
private float targetYRotation;
void Update()
{
setCameraLook();
setControllerLook();
}
void setControllerLook()
{
float xInput = Input.GetAxisRaw("Horizontal");
float yInput = Input.GetAxisRaw("Vertical");
if(xInput * xInput + yInput * yInput > 0)
{
targetControllerYRotation = Mathf.Atan2(xInput, yInput) * Mathf.Rad2Deg;
targetControllerYRotation -= yAngleOffset;
}
currentControllerYRotation = Mathf.SmoothDampAngle(currentControllerYRotation, targetControllerYRotation, ref controllerYRotationVelocity, controllerLookSmoothTime);
controller.transform.eulerAngles = new Vector3(0f, currentControllerYRotation, 0f);
}
void setCameraLook()
{
if (Input.GetMouseButton(1))
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
targetYRotation += mouseX * sensitivity;
targetXRotation -= mouseY * sensitivity;
targetXRotation = Mathf.Clamp(targetXRotation, xRotationLimits.x, xRotationLimits.y);
}
currentCameraXRotation = Mathf.SmoothDampAngle(currentCameraXRotation, targetXRotation, ref xRotationVelocity, lookSmoothTime);
currentCameraYRotation = Mathf.SmoothDampAngle(currentCameraYRotation, targetYRotation, ref yRotationVelocity, lookSmoothTime);
cameraPivot.eulerAngles = new Vector3(currentCameraXRotation, currentCameraYRotation, 0f);
Ray camRay = new Ray(cameraPivot.position, -cameraPivot.forward);
float maxDistance = cameraDistance;
if(Physics.SphereCast(camRay, 0.25f, out RaycastHit hitInfo, cameraDistance, camRaycastMask))
{
maxDistance = (hitInfo.point - cameraPivot.position).magnitude - 0.25f;
}
cameraTransform.localPosition = Vector3.forward * -(maxDistance - 0.1f);
yAngleOffset = Mathf.Atan2(cameraPivot.forward.z, cameraPivot.forward.x) * Mathf.Rad2Deg - 90f;
}
}
Could you do same but with a animated character?
Working with animations is a separate task. This series is focusing strictly on the base functionality of third person movement.
@@Rytech_Dev Is it possible that you could do a tutorial on how to add animations to that someday?
your old thumbnail looked better