I'm trying to move my camera with the player but I'm trying to move/lerp over time, so that the camera allows the player to get a small radius from the center as it gets up to speed, and the camera will "catch up" when the player slows or stops.
I've written code that increases the movement speed of the camera the further the player gets - ideally you should only be able to go a set radius before the camera just "follows" you.
This works, except it's too jittery when the player is moving. The larger the distance, the more noticeable the jitter. I've tried different speeds but nothing seems to satisfactorily solve the issue.
Player.cs Update()
:
var moveHorizontal = player.GetAxis("Move Horizontal") * rigidBody.mass;
var moveVertical = player.GetAxis("Move Vertical") * rigidBody.mass;
var forceDirection = new Vector3(moveHorizontal, 0, moveVertical);
rigidBody.AddForce(forceDirection);
Camera's Follow.cs
:
void Awake() {
offset = transform.position - player.transform.position;
}
void LateUpdate() {
var dist = Vector3.Distance(transform.position - offset, player.transform.position);
if (dist != 0) {
transform.position = Vector3.Lerp(
transform.position,
player.transform.position + offset,
Time.deltaTime * dist * speed);
}
}
Per the first answer, I've also tried setting my player's rigibody to "Interpolation" mode, and I update my follow script with the suggested smoothing code:
// Compute our exponential smoothing factor.
float blend = 1f - Mathf.Pow(1f - followSharpness, Time.deltaTime * 30f);
transform.position = Vector3.Lerp(
transform.position,
player.transform.position + offset,
blend);
This works initially, but I get awful jitter once my player collides with an object. Plus, interpolation mode appears more susceptible to framerate drops but that's an aspect of my game I haven't worked on yet.