简体   繁体   English

在 Unity 中拖动相机时如何防止鬼影移动?

[英]How to prevent ghost movement when dragging camera in Unity?

I have the below script attached to my main camera.我将以下脚本附加到我的主相机上。 Its for a VR app.它适用于 VR 应用程序。 The script allows me to rotate the camera when pressing down on the mouse button.该脚本允许我在按下鼠标按钮时旋转相机。

The script works but the problem is, while I have the mouse button pressed down the view slightly keeps rotating towards the left.该脚本有效,但问题是,当我按下鼠标按钮时,视图略微保持向左旋转。 When I release the mouse button the movement stops.当我松开鼠标按钮时,移动停止。

I cant find the cause of this "ghost" movement我找不到这个“鬼”运动的原因

public class DragCamera : MonoBehaviour
{
    // flag to keep track whether we are dragging or not
    bool isDragging = false;

    // starting point of a camera movement
    float startMouseX;
    float startMouseY;

    // Camera component
    Camera cam;

    // Use this for initialization
    void Start()
    {
        // Get our camera component
        cam = GetComponent<Camera>();
        Cursor.visible = false;
    }

    // Update is called once per frame
    void Update()
    {

        // if we press the left button and we haven't started dragging
        if (Input.GetMouseButtonDown(0) && !isDragging)
        {
            // set the flag to true
            isDragging = true;

            // save the mouse starting position
            startMouseX = Input.mousePosition.x;
            startMouseY = Input.mousePosition.y;
        }
        // if we are not pressing the left btn, and we were dragging
        else if (Input.GetMouseButtonUp(0) && isDragging)
        {
            // set the flag to false
            isDragging = false;
        }
    }

    void LateUpdate()
    {
        // Check if we are dragging
        if (isDragging)
        {
            //Calculate current mouse position
            float endMouseX = Input.mousePosition.x;
            float endMouseY = Input.mousePosition.y;

            //Difference (in screen coordinates)
            float diffX = endMouseX - startMouseX;
            float diffY = endMouseY - startMouseY;

            //New center of the screen
            float newCenterX = Screen.width / 2 + diffX;
            float newCenterY = Screen.height / 2 + diffY;

            //Get the world coordinate , this is where we want to look at
            Vector3 LookHerePoint = cam.ScreenToWorldPoint(new Vector3(newCenterX, newCenterY, cam.nearClipPlane));

            //Make our camera look at the "LookHerePoint"
            transform.LookAt(LookHerePoint);

            //starting position for the next call
            startMouseX = endMouseX;
            startMouseY = endMouseY;
        }
    }
}
 float newCenterX = Screen.width / 2 + diffX; float newCenterY = Screen.height / 2 + diffY;

You may want to consider using parenthesis around math involving multiplication/division, and addition/subtracting.您可能需要考虑在涉及乘法/除法和加法/减法的数学周围使用括号。 This helps prevent the compiler from interpreting float newCenterX = Screen.width / 2 + diffX;这有助于防止编译器解释float newCenterX = Screen.width / 2 + diffX; as作为

float newCenterX = Screen.width / (2 + diffX);

instead of the intended而不是预期的

float newCenterX = (Screen.width / 2) + diffX;

When using the mono compiler (unity) I've personally found that it takes division by integers literally.当使用 mono 编译器(统一)时,我个人发现它实际上需要整数除法。 ie. IE。 12f / 2 and rounds it to the nearest integer some times. 12f / 2并将其舍入到最近的 integer 几次。 Try to explicitly divide by the same primitive type such as 12f / 2f尝试显式除以相同的原始类型,例如12f / 2f

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

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