C# 玩家移动时摄像机抖动/抖动

C# 玩家移动时摄像机抖动/抖动,c#,visual-studio,unity3d,C#,Visual Studio,Unity3d,我制作了一个摄影机脚本,它跟随玩家使用lerp使其看起来更平滑,但由于某些原因,在某些突然出现的区域行走时,它看起来很滞后 玩家行走的地面是tilemap,起初,我认为是微小的细胞间隙造成了问题,但即使我将地面改为一个实心块,这种情况仍然会发生。所以我得出结论,一定是相机脚本导致了这个问题 下面是我所说内容的视频剪辑: 我认为问题源于我的相机脚本的这一部分: void SetTargetPos() { // By default the target x and y coordinat

我制作了一个摄影机脚本,它跟随玩家使用lerp使其看起来更平滑,但由于某些原因,在某些突然出现的区域行走时,它看起来很滞后

玩家行走的地面是tilemap,起初,我认为是微小的细胞间隙造成了问题,但即使我将地面改为一个实心块,这种情况仍然会发生。所以我得出结论,一定是相机脚本导致了这个问题

下面是我所说内容的视频剪辑:

我认为问题源于我的相机脚本的这一部分:

 void SetTargetPos()
{
    // By default the target x and y coordinates of the camera are it's current x and y coordinates.
    targetX = transform.position.x;
    targetY = transform.position.y;
    // If the player has moved beyond the x margin...
    if (CheckXMargin())
    {
        // ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
       targetX = Mathf.Lerp(transform.position.x, transformPlayer.position.x, xSmooth * Time.deltaTime); 

    }

    // If the player has moved beyond the y margin...
    if (CheckYMargin())
    {
        // ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
       targetY = Mathf.Lerp(transform.position.y, transformPlayer.position.y, ySmooth * Time.deltaTime);


    }

        // The target x and y coordinates should not be larger than the maximum or smaller than the minimum.
        targetX = Mathf.Clamp(targetX, currentMinBounds.x, currentMaxBounds.x);
        targetY = Mathf.Clamp(targetY, currentMinBounds.y, currentMaxBounds.y);
   
    
    // Set the camera's position to the target position with the same z component.
    transform.position = new Vector3(targetX, targetY, transform.position.z);

    TestOutOfCamBounds();
}
顺便说一句,我在不同的Unity项目上有完全相同的脚本,具有相同的变量输入。有一个小小的区别,第二个项目中所有东西的总体规模都比第一个项目小得多。但它在那个项目上运行得非常好。我也尝试过用Smoothdamp代替Lerp,但仍然没有成功。以下是另一个项目的视频剪辑:

任何帮助都将不胜感激

如果您想在此处查看整个脚本,它是:

    public class Camera_Controller : MonoBehaviour
{
    private static Camera_Controller _instance;
    public static Camera_Controller instance;
    [Header("Put player here")]
    public GameObject player;
    public Transform transformPlayer;
    [Space]
    Vector2 currentMinBounds;
    Vector2 currentMaxBounds;
    public Vector3 targetPos;

    [Space]
    [Header("Camera Properties")]
    public float xMargin = 1f;
    public float yMargin = 1f;
    public float xSmooth = 8f;
    public float ySmooth = 8f;
    public Vector3 velocity = Vector3.zero;

    private float targetY;
    private float targetX;

    [Header("Smooth Transition")]
    public Vector3 oldPosition;
    public Vector3 targetPosition;
    public float transitionsTime;
    public bool switchingCamera;

    private void Awake()
    {


        if (_instance != null && _instance != this)
        {
            Destroy(gameObject);
            return;
        }
        else
        {
            _instance = this;
        }

        instance = _instance;
    }

    void Start()
    {
        targetPos = transform.position;
    }

    private bool CheckXMargin()
    {
        // Returns true if the distance between the camera and the player in the x axis is greater than the x margin.
        return Mathf.Abs(transform.position.x - transformPlayer.position.x) > xMargin;
    }


    private bool CheckYMargin()
    {
        // Returns true if the distance between the camera and the player in the y axis is greater than the y margin.
        return Mathf.Abs(transform.position.y - transformPlayer.position.y) > yMargin;
    }
    void Update()
    {
        if (!switchingCamera)
        {
            SetTargetPos();
        }

    }


    public void SetCamBounds(Vector2 minBounds, Vector2 maxBounds) //Called from Game Events Trough WarpController as trigger
    {
        currentMinBounds = minBounds;
        currentMaxBounds = maxBounds;
    }
    //SetTargetPos() should be causing the problem
    void SetTargetPos()
    {
        // By default the target x and y coordinates of the camera are it's current x and y coordinates.
        targetX = transform.position.x;
        targetY = transform.position.y;
        // If the player has moved beyond the x margin...
        if (CheckXMargin())
        {
            // ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
           targetX = Mathf.Lerp(transform.position.x, transformPlayer.position.x, xSmooth * Time.deltaTime); 

        }

        // If the player has moved beyond the y margin...
        if (CheckYMargin())
        {
            // ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
           targetY = Mathf.Lerp(transform.position.y, transformPlayer.position.y, ySmooth * Time.deltaTime);


        }

            // The target x and y coordinates should not be larger than the maximum or smaller than the minimum.
            targetX = Mathf.Clamp(targetX, currentMinBounds.x, currentMaxBounds.x);
            targetY = Mathf.Clamp(targetY, currentMinBounds.y, currentMaxBounds.y);
       
        
        // Set the camera's position to the target position with the same z component.
        transform.position = new Vector3(targetX, targetY, transform.position.z);

        TestOutOfCamBounds();
    }

    void TestOutOfCamBounds() //Set Camera some boundaries.
    {
        if (targetPos.x <= currentMinBounds.x)
        {
            targetPos.x = currentMinBounds.x;
        }
        if (targetPos.x >= currentMaxBounds.x)
        {
            targetPos.x = currentMaxBounds.x;
        }
        if (targetPos.y <= currentMinBounds.y)
        {
            targetPos.y = currentMinBounds.y;
        }
        if (targetPos.y >= currentMaxBounds.y)
        {
            targetPos.y = currentMaxBounds.y;
        }
    }

}

第二个视频是2d,第一个是3d。也可以尝试调整z位置

targetZ = Mathf.Lerp(transform.position.z, transformPlayer.position.z, zSmooth * Time.deltaTime); 

我找到了解决这一切的办法!
我将更新功能更改为LateUpdate。。。很抱歉浪费了您的时间

请尝试在后期更新中调用cammer移动,例如下面的示例

private void LateUpdate() {
    SetTargetPos();
}

这是在更新方法之后调用的,可能有助于减少cammer抖动。

谢谢!我完成学业后一定要试试。但我不明白的是,你怎么知道它是3D的?第一个项目中的所有精灵和对象都是二维的,我甚至使用Unity 2D Template创建了该项目抱歉,但这不起作用。