简体   繁体   English

为什么我的角色/相机在Unity 2d中口吃?

[英]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! EDIT2:没关系,在rigidbody2d中打开插值可以解决问题! Thanks for your help! 谢谢你的帮助!

The studdering is not caused due to the Update() function! 钉书不是由于 Update() 函数引起的!

The stuttering is probably caused because of the Lerp() function, you would rather use the SmoothDamp() . 口吃可能是由于Lerp()函数引起的,您宁愿使用SmoothDamp()

Like this 像这样

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

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

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