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().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!
↧