根据鼠标位置围绕对象Unity3D旋转相机 [英] Rotate camera based on mouse postitions around a object Unity3D
问题描述
好的,所以我走了这么远:
Okay, So I have come this far:
public class CameraScript : MonoBehaviour {
public void RotateCamera()
{
float x = 5 * Input.GetAxis("Mouse X");
float y = 5 * -Input.GetAxis("Mouse Y");
Camera.mainCamera.transform.parent.transform.Rotate (y,x,0);
}
}
我的相机有一个父对象,我会根据鼠标位置对其进行旋转.唯一的问题是我只能用鼠标滑动来旋转对象.如果仅单击对象旁边的位置,如何旋转基于我的相机的相机所连接的对象.预先感谢!
My camera has a parent which I rotate based on my mouse position. The only problem is that I can only swipe with the mouse to rotate the object. How can I rotate the object which is my camera is attached to based on my mouse position if I just click next to the object. Thanks in advance!
推荐答案
对于键盘和操纵杆输入,该值将在-1 ... 1范围内. 如果将轴设置为鼠标移动的增量,则鼠标的增量为 乘以轴灵敏度,范围不为-1 ... 1. 统一文档
The value will be in the range -1...1 for keyboard and joystick input. If the axis is setup to be delta mouse movement, the mouse delta is multiplied by the axis sensitivity and the range is not -1...1. Unity Document
注意:此链接很有用,请检查它.
Note: This link is usefull please check it.
所以您需要像这样更改代码.
So you need to change your code like this.
public void RotateCamera()
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); // Gets mouse position to Unity World coordinate system
Camera.mainCamera.transform.parent.transform.Rotate (mousePosition);
}
如果有问题,您可以这样做
if there are problem you can do like this
public void RotateCamera()
{
Vector3 position = new Vector3(Input.mousePosition.x, Input.mousePosition.y,0);
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(position); // Gets mouse position to Unity World coordinate system
Camera.mainCamera.transform.parent.transform.Rotate (mousePosition);
}
另一个选择是 rotateTowards .
public float speed=10; //any value > 0
public void RotateCamera()
{
Vector3 targetDir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.mainCamera.transform.parent.transform.position;
float step = speed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0F);
Camera.mainCamera.transform.parent.transform.rotation = Quaternion.LookRotation(newDir);
}
也许是语法错误,我不检查它们.
Maybe some syntax errors, i don't check them.
这篇关于根据鼠标位置围绕对象Unity3D旋转相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!