一种比较匀速的差值运算

来源:互联网 发布:a星寻路算法介绍 编辑:程序博客网 时间:2024/05/01 14:09
//x0 is the follower's old position, y0 is the target's old position, yt is the target's new position, t is the elapsed time, and k is the lerp rate, as in (1 - Mathf.Exp(-k * Time.deltaTime)) before. You used 20 for that. The return value is the follower's new position. So you call it much like Vector3.Lerp, except you pass the target's old and new positions, rather than just its new position.    //The maths assumes that the target is moving with constant velocity over the time slice, so if the target is changing velocity rapidly as well it's still going to jitter a bit. But in any case it should be a lot more smooth than plain Lerp was, and other than being smooth, it has exactly the same behaviour (e.g. response to different k values, following distance, etc).    Vector3 SuperSmoothLerp(Vector3 x0, Vector3 y0, Vector3 yt, float t, float k)    {        Vector3 f = x0 - y0 + (yt - y0) / (k * t);        //Debug.Log("差值是:" + Mathf.Exp(-k * t).ToString());        return yt - (yt - y0) / (k * t) + f * Mathf.Exp(-k * t);    }transform.position = SuperSmoothLerp(transform.position, oldWantTo, to, cameraSpeed, Time.deltaTime);            oldWantTo = transform.position;
float lerp = 1f - Mathf.Exp(-cameraRotSpeed * Time.deltaTime);            transform.rotation = Quaternion.Lerp(transform.rotation, nowQu, lerp);

来自:https://forum.unity3d.com/threads/how-to-smooth-damp-towards-a-moving-target-without-causing-jitter-in-the-movement.130920/

0 0