I'm bad at math and I'm looking to make a cameras rotation ease into the players, in this case a car so that it feels more noticeable when it drifts.
The concept code would look something like this:
car.rotation = this.rotation / 15
sadly this isn't how the language works and quaternions are too complicated for me to understand.
If anyone could share any methods or code with me to do what I'm trying to do please tell.
I'm bad at math and I'm looking to make a cameras rotation ease into the players, in this case a car so that it feels more noticeable when it drifts.
The concept code would look something like this:
car.rotation = this.rotation / 15
sadly this isn't how the language works and quaternions are too complicated for me to understand.
If anyone could share any methods or code with me to do what I'm trying to do please tell.
Share Improve this question asked Mar 20 at 16:35 paperboymanpaperboyman 253 bronze badges 1 |2 Answers
Reset to default 1For a smoother camera you probably want to use SmoothDampAngle as Lerp can be to rough and doesn't account for car velocity.
First you need to get the rotation.
- To get the current rotation of the camera on the Y axis use:
transform.eulerAngles.y
- To get the car's rotation on the Y axis you can use:
car.eulerAngles.y
Second you need to interpolate the transition.
To get the camera's current Y rotation use:
currentY
To get the car's Y rotation use:
targetY
To store the angular velocity use:
velocity
(it's an internal variable)To smooth the time use:
smoothTime
Complete function: float newY = Mathf.SmoothDampAngle(currentY, targetY, ref velocity, smoothTime);
Third you do the transition
To convert the smooth transition into a Quaternion(what Unity uses for rotation):
Quaternion.Euler(0, newY, 0)
To apply the rotation:
transform.rotation = Quaternion.Euler(0, newY, 0);
Final code
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform car; // Reference to the car's Transform.
public float smoothTime = 0.3f; // The time it takes to reach the target rotation smoothly.
private float velocity = 0.0f; // Stores the current angular velocity (needed for SmoothDampAngle).
void Update()
{
if (car == null) return; // Safety check to avoid errors.
// Get the current Y-axis rotation of the camera
float currentY = transform.eulerAngles.y;
// Get the target Y-axis rotation of the car
float targetY = car.eulerAngles.y;
// Smoothly interpolate the rotation angle using SmoothDampAngle
float newY = Mathf.SmoothDampAngle(currentY, targetY, ref velocity, smoothTime);
// Apply the new rotation to the camera
transform.rotation = Quaternion.Euler(0, newY, 0);
}
}
Adjusting Smoothness
Increase
smoothTime
(e.g., 0.5f) → Slower, smoother rotation.Decrease
smoothTime
(e.g., 0.1f) → Faster response, but less smooth.
I hope this helps :D
You might consider using the Cinemachine package. Without performing complicated computations and using its components' features you may achieve close results.
Lerp
is commonly used to change value over time creating animation, see e.g. this unity question or here is some implementation. – Sinatr Commented Mar 20 at 16:43