- Home /
Slerping Camera Top-Down view, how to keep it over player?
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);
}
}
Nice atmosphere in your game. First, I don't think you need Slerp() here. It seems more like a Lerp() situation. Have you tried increasing the speed of the Slerp()? Typically the last parameter of the Slerp/Lerp is written as '`Time.deltaTime * speed`'. In your case by dividing by 2, your speed is 0.5. Try replacing the last parameter and play with different speed values.
THanks -- the graphics deserve a HUGE shout out to $$anonymous$$ichael O (search "Top Down $$anonymous$$obile" in the asset store). All of his work is A$$anonymous$$AZING, and this is one of them, the layout etc is actually his demo scene.
However, increasing the speed keeps the wobble in the camera. Time.deltaTime keeps the wobble just a bit visible, so anything faster would be worse. But even then, the camera still moves too far to be real "top-down" anymore.
How do you think a lerp would work better?
The 'S' in Slerp stands for 'spherical', and I just don't see any spherical movement here.
I thought about doing an "Average" thing, but figured there had to be a simpler way. I thought about trying to lerp only the y axis on the myTransform.position, but couldn't figure out how to do it correctly.
This is one of those things that, unless you've faced it before, you will just have to play with some concepts. I'd work with trying to average the left/right movement as a next step.
Answer by whebert · Jun 18, 2013 at 06:15 PM
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));
}
}
That may work -- I did not test it.
Ins$$anonymous$$d, I found an animation that didn't itself have the wobble, or something, because I got it working. Not 100% sure what. I did read something about how baking the rotation when yo uset up the animation may fix it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104400im_/https://answers.unity.com/themes/thub/images/avi.jpg)