在 Unity Cardboard VR 中移动鼠标

发布于 2025-01-11 08:39:18 字数 1519 浏览 0 评论 0原文

我想通过推动鼠标在 Cardboard VR 环境中移动相机,但我不知道如何使其工作。有什么想法吗?

下面是移动相机的简单代码片段。

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

public class MouseMove : MonoBehaviour
{
    public Transform cam;
    protected Vector3 left;
    protected Vector3 forward;
    public float scalemove;
    // Start is called before the first frame update
    void Start()
    {
        left = new Vector3(-1, 0, 0);
        forward = new Vector3(0, 0, 1);
        left = left * scalemove;
        forward = forward * scalemove;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetAxis("Mouse X") < 0)
        {
            cam.Translate(left * Time.deltaTime);
            //Code for action on mouse moving left
            print("Mouse moved left");
        }
        if (Input.GetAxis("Mouse X") > 0)
        {
            cam.Translate(-left * Time.deltaTime);
            //Code for action on mouse moving right
            print("Mouse moved right");
        }
        if (Input.GetAxis("Mouse Y") < 0)
        {
            cam.Translate(-forward * Time.deltaTime);
            //Code for action on mouse moving left
            print("Mouse moved down");
        }
        if (Input.GetAxis("Mouse Y") > 0)
        {
            cam.Translate(forward * Time.deltaTime);
            //Code for action on mouse moving right
            print("Mouse moved up");
        }
    }
}

我在Windows10上使用Unity2019。

谢谢你的建议。

I want to move camera in Cardboard VR environment by pushing mouse, but I cannot figure out how to make it work. Any Ideas?

Below is a simple code piece to move camera.

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

public class MouseMove : MonoBehaviour
{
    public Transform cam;
    protected Vector3 left;
    protected Vector3 forward;
    public float scalemove;
    // Start is called before the first frame update
    void Start()
    {
        left = new Vector3(-1, 0, 0);
        forward = new Vector3(0, 0, 1);
        left = left * scalemove;
        forward = forward * scalemove;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetAxis("Mouse X") < 0)
        {
            cam.Translate(left * Time.deltaTime);
            //Code for action on mouse moving left
            print("Mouse moved left");
        }
        if (Input.GetAxis("Mouse X") > 0)
        {
            cam.Translate(-left * Time.deltaTime);
            //Code for action on mouse moving right
            print("Mouse moved right");
        }
        if (Input.GetAxis("Mouse Y") < 0)
        {
            cam.Translate(-forward * Time.deltaTime);
            //Code for action on mouse moving left
            print("Mouse moved down");
        }
        if (Input.GetAxis("Mouse Y") > 0)
        {
            cam.Translate(forward * Time.deltaTime);
            //Code for action on mouse moving right
            print("Mouse moved up");
        }
    }
}

I am using Unity2019 on Windows10.

Thanks for your advice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

起风了 2025-01-18 08:39:18

只需给相机一个空的父级,使用鼠标更改父级的位置,代码就可以运行。最好的!

just give the camera an empty parent, use the mouse to change the position of the parent, and the code runs OK. Best!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文