- Home /
Rigidbody jittering with camera follow
Hi
I have a rigidbody player that is being moved by setting the velocity and rigidbody.MoveRotation. (Example code below)
void FixedUpdate(){
r.MoveRotation(r.rotation * newRot);
r.velocity = transform.forward * currentSpeed;
}
I also have a camera following this player with this script:
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Smooth Follow 3D CSharp")]
public class SmoothFollow3D : MonoBehaviour
{
// The target we are following
public Transform target;
// The distance in the x-z plane to the target
public float distance = 10.0f;
// the height we want the camera to be above the target
public float height = 5.0f;
// How much we
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;
void LateUpdate ()
{
// Early out if we don't have a target
if (!target)
return;
Vector3 followpos = new Vector3(0.0f, height, -distance);
Quaternion lookrotation = Quaternion.identity;
lookrotation.eulerAngles = new Vector3(0.0f, 0.0f, 0.0f);
Matrix4x4 m1 = Matrix4x4.TRS(target.position, target.gameObject.GetComponent<Rigidbody>().rotation, Vector3.one);
Matrix4x4 m2 = Matrix4x4.TRS(followpos, lookrotation, Vector3.one);
Matrix4x4 combined = m1 * m2;
// THE WAY TO GET POSITION AND ROTATION FROM A MATRIX4X4:
Vector3 position = combined.GetColumn(3);
Quaternion rotation = Quaternion.LookRotation(
combined.GetColumn(2),
combined.GetColumn(1)
);
Quaternion wantedRotation = rotation;
Quaternion currentRotation = transform.rotation;
Vector3 wantedPosition = position;
Vector3 currentPosition = transform.position;
currentRotation = Quaternion.Lerp(currentRotation, wantedRotation, rotationDamping * Time.deltaTime);
currentPosition = Vector3.Lerp(currentPosition, wantedPosition, heightDamping * Time.deltaTime);
transform.localRotation = currentRotation;
transform.localPosition = currentPosition;
}
}
(I have my own smooth follow that looks pretty similar but I grabbed this on off the internet).
No Matter what I do my player still jitters when I rotate it (rotation is based on the mouse position on the screen). I tried interpolate/extrapolate, I tried lateupdate on the camera and smoothdamp instead of lerp.
How do I fix this? What is the ideal setup for something like this?
Thanks!
Answer by tuinal · Aug 14, 2019 at 01:41 AM
The MouseOrbitImproved script floating round the net (quick google) is a solid starting point for a 3rd person camera. The script you've got probably works but it's an overcomplicated way of doing a camera follow / rotate.
I think in terms of fixing this, currentRotation and currentPosition want to be from the frame before, not the current frame. Move their declarations outside of the update method, then remove the lines you're setting them with (except the 2 at the very end of the method - and you might need to set them to transform.position/rotation in Start() to avoid a null reference), and give that a try?
@tuinal I tried using the previous frame rotation/position and I still get a slight jitter.... Whats the right way to go about this? It seems like everyone has a different solution... From what I understand camera in lateUpdate, player physics in fixedUpdate and Rigidbody Interpolate is the correct way to go. But then how can objects (ie Trailrenderers) attached to the rigidbody be rendered correctly if they are using update but being interpolated? Thanks again for the answer!
I changed the fixed timestamp to 0.015 and its way better, but that is definitely not the right way to fix this...
Your answer
Follow this Question
Related Questions
RB Interpolation 1 Answer
Weird jitter on Android using Character Controller 0 Answers
How to move a kinematic rigidbody smoothly? 3 Answers
Jittery Rigidbody 4 Answers
Character controller with Rigidbody to apply force 0 Answers