繁体   English   中英

unity 3d 看某个角度时抖动相机

[英]Shaking camera when looking at certain angle in unity 3d

我的相机 controller 有一个奇怪的问题,我不明白为什么。 当我朝某些方向看时,相机开始抖动。 相机的旋转在这一刻似乎变化很快,但我不明白为什么。

这是我使用的代码(在更新中调用)。

void PhotoMode()
    {
        Vector3 newPos = transform.position;
        //déplacement avant
        if(Input.GetKey(InputManager.IM.forward))
        {
            newPos += cam.transform.forward  * moveSpeedPhoto * Time.unscaledDeltaTime;
        } 

        //déplacement arrière
        if(Input.GetKey(InputManager.IM.backward))
        {
            newPos -= cam.transform.forward * moveSpeedPhoto * Time.unscaledDeltaTime;
        } 

        //déplacement gauche
        if(Input.GetKey(InputManager.IM.left))
        {
            newPos += -transform.right * moveSpeedPhoto * Time.unscaledDeltaTime;
        }

        //déplacement droite
        if(Input.GetKey(InputManager.IM.right))
        {   
            newPos += transform.right * moveSpeedPhoto * Time.unscaledDeltaTime;
        }

        float x = Input.GetAxis("Mouse X");
        float y = Input.GetAxis("Mouse Y");

        x *= photoRotSpeed * Time.unscaledDeltaTime;
        y *= photoRotSpeed * Time.unscaledDeltaTime;

        currentX = Mathf.Lerp(currentX, x, smoothTime * Time.unscaledDeltaTime);
        currentY = Mathf.Lerp(currentY, y, smoothTime * Time.unscaledDeltaTime);

        float rotationX = transform.localEulerAngles.y + currentX * photoRotSpeed;

        rotationY += currentY * photoRotSpeed;
        rotationY = Mathf.Clamp(rotationY, -90+65, 90+65);

        transform.localRotation = Quaternion.Euler(new Vector3(-rotationY, rotationX, 0));

        transform.position = newPos;
    }

在此处输入图像描述

我设法使它与这个一起工作:(游戏object中的凸轮,它有脚本)

void PhotoMode()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            dontMove = !dontMove;
            screenshotManager.photoModeCanvas.SetActive(dontMove);

            if(!dontMove && screenshotManager.focusDistanceIndicatorToggle.isOn && screenshotManager.distanceIndicator.activeSelf)
            {
                screenshotManager.FocusDistanceIndicator(false);
            }

            if(dontMove && screenshotManager.focusDistanceIndicatorToggle.isOn && !screenshotManager.distanceIndicator.activeSelf)
            {
                screenshotManager.FocusDistanceIndicator(true);
            }
        }

        if(dontMove)
        {
            Cursor.lockState = CursorLockMode.None;
            return;
        }

        Cursor.lockState = CursorLockMode.Locked;

        Vector3 newPos = transform.position;
        float scroll = Input.GetAxis("Mouse ScrollWheel");

        moveSpeedPhoto += scroll*10;
        moveSpeedPhoto = Mathf.Clamp(moveSpeedPhoto, 0, 100);
        //déplacement avant
        if(Input.GetKey(InputManager.IM.forward))
        {
            newPos += cam.transform.forward  * moveSpeedPhoto * Time.unscaledDeltaTime;
        } 

        //déplacement arrière
        if(Input.GetKey(InputManager.IM.backward))
        {
            newPos -= cam.transform.forward * moveSpeedPhoto * Time.unscaledDeltaTime;
        } 

        //déplacement gauche
        if(Input.GetKey(InputManager.IM.left))
        {
            newPos += -transform.right * moveSpeedPhoto * Time.unscaledDeltaTime;
        }

        //déplacement droite
        if(Input.GetKey(InputManager.IM.right))
        {   
            newPos += transform.right * moveSpeedPhoto * Time.unscaledDeltaTime;
        }

        float x = Input.GetAxis("Mouse X") * photoRotSpeed * Time.unscaledDeltaTime;
        float y = Input.GetAxis("Mouse Y") * photoRotSpeed * Time.unscaledDeltaTime;

        rotationX -= y;
        rotationX = Mathf.Clamp(rotationX, -90, 90);

        cam.transform.localRotation = Quaternion.Euler(rotationX, 0f, 0f);
        this.transform.Rotate(Vector3.up * x);

        transform.position = newPos;
    }

暂无
暂无

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

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