简体   繁体   中英

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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