简体   繁体   中英

Why does my character/camera stutter in Unity 2d?

Ever since I've added this new camera script, my character (which the camera is trying to follow) keeps on stuttering when it is moving. If I keep the character still, then it won't stutter anymore.

Here's a video of what I mean.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CameraFollow : MonoBehaviour
{   
public Transform lookat;

private bool smooth = true;
private float smoothSpeed = 0.1f;
private Vector3 offset = new Vector3 (0, 0, -6.5f); 

void LateUpdate() {

    Vector3 desiredPosition = lookat.transform.position + offset;

    if (smooth) {
        transform.position = Vector3.Lerp (transform.position, desiredPosition, smoothSpeed);
    } else {
        transform.position = desiredPosition;
    }

}

}

Please help - it's driving me crazy! EDIT: Also ignore the error at the bottom - it's part of something else I'm working on.

EDIT2: Never mind, turning on interpolation in rigidbody2d fixed it! Thanks for your help!

The studdering is not caused due to the Update() function!

The stuttering is probably caused because of the Lerp() function, you would rather use the SmoothDamp() .

Like this

transform.position = Vector3.SmoothDamp(transform.position, desiredPosition, smoothSpeed);

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