简体   繁体   English

Unity 3D 中的抖动相机移动

[英]Jittery camera movement in Unity 3D

I am making an endless runner game like temple run.我正在制作一个无尽的跑步游戏,比如寺庙跑。 The camera follows the player at an offset.相机在偏移处跟随玩家。 When the camera gets too close to the player, the jitteriness becomes very obvious.当相机离玩家太近时,抖动变得非常明显。 I think when the camera is far away from the player(at the very start of the game, when the offset has not been reached), it's still jittery.我认为当相机远离玩家时(在游戏开始时,尚未达到偏移量时),它仍然很紧张。

How can I resolve the jitteriness?如何解决抖动?

The code for camera:摄像头代码:

private void Start()
    {
        tempPos = target.position;
        offset = transform.position - target.position;

    }

    // Update is called once per frame
    void LateUpdate()
    {

        Vector3 followPos = target.position - target.forward * trailDistance;
        followPos.y += heightOffset;
        tempPos += (followPos - transform.position) * cameraDelay;
        //var targetRotation = Quaternion.LookRotation(target.transform.position - transform.position);
        //transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
        Vector3 smoothedPosition = Vector3.SmoothDamp(transform.position, tempPos, ref velocity, smoothSpeed);
        transform.position = smoothedPosition;
        transform.LookAt(target.transform);
   
    }

There is an easier way to do this that I use.我使用了一种更简单的方法来做到这一点。 It also has an awesome smooth/delay effect.它还具有令人敬畏的平滑/延迟效果。 This is also what you would use in a Multiplayer Scenario when the code is really jittery due to lag.这也是您在多人游戏场景中使用的,当代码由于延迟而非常紧张时。

float delay = 2f;// Play around with me

void FixedUpdate() 
{
    Vector3 followPos = target.position - target.forward * trailDistance;
    transform.position = Vector3.Lerp(transform.position, followPos, delay);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM