【问题标题】:Camera shaking/Jitters when Player moves玩家移动时相机抖动/抖动
【发布时间】:2021-03-19 22:57:24
【问题描述】:

我使用 lerp 制作了一个跟随玩家的相机脚本,让它看起来更流畅,但由于某些原因,在某些突然的区域行走时它看起来很迟钝。

玩家行走的地面是瓷砖地图,起初我以为是一些微小的单元格间隙导致了问题,但即使我将地面改为一个实心块,它仍然会发生。所以我得出结论,一定是相机脚本导致了这个问题。

这是我正在谈论的视频剪辑:https://youtu.be/mmBMHWuHpxo

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

 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,但仍然没有成功。这是另一个项目的视频剪辑:https://youtu.be/baJmKehYfG0

任何帮助将不胜感激。

如果你想在这里查看整个脚本:

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

}

【问题讨论】:

    标签: c# visual-studio unity3d


    【解决方案1】:

    尝试在后期更新中调用摄像头运动,示例如下。

    private void LateUpdate() {
        SetTargetPos();
    }
    

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 谢谢!我一放学就一定要试试。但是我不明白的东西,你怎么知道它是3D的?第一个项目中所有的精灵和对象都是 2D 的,我什至使用 Unity 2D 模板制作了项目
      【解决方案3】:

      我找到了解决办法! 我把更新函数改成 LateUpdate()...抱歉浪费你的时间

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多