在 Unity Cardboard VR 中移动鼠标
我想通过推动鼠标在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需给相机一个空的父级,使用鼠标更改父级的位置,代码就可以运行。最好的!
just give the camera an empty parent, use the mouse to change the position of the parent, and the code runs OK. Best!