Here’s a video of the issue – as the player walks forward, the camera starts to lag behind the player due to the slerp I’m using, and camera starts showing less of a top-down view and more 3rd person. I’d like to change this so that the camera stays more overhead.
http://www.sfbaystudios.com/slerpissue.mov
However, I use the slerp in the code to keep the camera from moving left/right just a bit with every step the player takes. It uses Mechanim Root Motion for the walking, so every step the player officially is moving forward but kind of goes a bit left and a bit right. I use the slerp to smooth out the motion and keep the camera from wobbling.
Any idea how I can change this?
using UnityEngine;
using System.Collections;
public class AttachCamera : MonoBehaviour
{
Transform myTransform;
public Transform target;
public Vector3 offset = new Vector3(0, 5, -5);
void Start()
{
myTransform = this.transform;
}
void Update()
{
myTransform.position = Vector3.Slerp(myTransform.position, target.position + target.TransformDirection(offset), (Time.deltaTime / 2));
myTransform.LookAt(target);
}
}
I haven’t fully tested this, but something like this may work in your situation, tweaked of course. If you had a smooth follower, not a camera but an empty game object, that followed your character (target) around with sort of a low-pass filter for position and rotation, then had your camera follow the smooth follower like you are following the character now, but without the Lerp/Slerp you are using on the camera, just straight up follow. The following scirpt may work as the low-pass filtered smooth follower - I am using the Lerp and Slerp as usual, but they shouldn’t react much to very small changes, and would progressively react more for larger changes, clamped on both ends.
using UnityEngine;
using System.Collections;
public class SmoothFollower : MonoBehaviour {
public Transform target;
// Use this for initialization
void Start () {
transform.position = target.position;
transform.rotation = target.rotation;
}
// Update is called once per frame
void Update () {
transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime * Mathf.Clamp((target.position - transform.position).sqrMagnitude * 8, .1f, 5));
transform.rotation = Quaternion.Slerp(transform.rotation, target.rotation, Time.deltaTime * Mathf.Clamp(Vector3.Angle(target.forward, transform.forward)/90 * 4, .1f, 5));
}
}