简体   繁体   English

相机跟随导致玩家口吃 Unity 2D

[英]Camera Follow Causing Player To Stutter Unity 2D

I'm currently trying to make the camera follow the player smoothly.我目前正在尝试使相机顺利跟随玩家。 The script works fine but the problem is that this script is causing the player to stutter at a certain point.该脚本工作正常,但问题是该脚本导致玩家在某个点口吃。 For example, if the player is at X:3, the player would stutter but then if the player was at X:-6, the player would stop stuttering.例如,如果玩家在 X:3,玩家会口吃,但如果玩家在 X:-6,玩家会停止口吃。 I'm 100% sure that this script is the problem because if I remove the script, the player stops stuttering.我 100% 确定这个脚本是问题所在,因为如果我删除脚本,播放器就会停止口吃。

Here is the camera follow script:这是相机跟随脚本:

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

public class CameraFollowing : MonoBehaviour
{
    [SerializeField]
    private Transform target;

    [SerializeField]
    private Vector3 cameraOffset;

    [SerializeField]
    private float followSpeed = 10f;

    [SerializeField]
    private float xMin = 0f;

    private Vector3 velocity = Vector3.zero;

    private void FixedUpdate()
    {
        Vector3 targetPos = target.position + cameraOffset;
        Vector3 clampedPos = new Vector3(Mathf.Clamp(targetPos.x, xMin, float.MaxValue), targetPos.y, targetPos.z);
        Vector3 smoothPos = Vector3.SmoothDamp(transform.position, clampedPos, ref velocity, followSpeed * Time.fixedDeltaTime);

        transform.position = smoothPos;
    }
}

If you know the answer or possible causes please tell me, I'm trying to release, publish, etc, this game by the end of this year.如果您知道答案或可能的原因,请告诉我,我正在努力在今年年底之前发布、发布等游戏。 Thanks: :D感谢:D

By stutter I think you mean shake or tremble (not familiar with the stutter word).口吃我认为你的意思是颤抖或颤抖(不熟悉口吃这个词)。 I would try to adjust to the docs example.我会尝试适应文档示例。 Use void Update() or LateUpdate() instead of FixedUpdate() .使用void Update()LateUpdate()而不是FixedUpdate()

You may want to use void LateUpdate() if you want your game to be accurate.如果您希望游戏准确,您可能需要使用void LateUpdate() This method will be called after the input is detected, so it will react better.这个方法会在检测到输入后调用,所以反应会更好。 I believe that this is the better choice, because it will be more accurate than Update() .我相信这是更好的选择,因为它会比Update()更准确。

I would also keep track of where in the scene the shake starts if its from a determined point on, and check for some misplaced collider near the position where the undesired shake starts.如果抖动从确定的点开始,我还将跟踪场景中的哪个位置,并检查 position 附近是否有一些错位的对撞机,从那里开始出现不希望的抖动。

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

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