解决Unity鼠标坐标点转成世界坐标系坐标点
2016-12-06 10:20
573 查看
第一种:
这是一种发射线,获取当前点击的物体(具有碰撞器)的坐标点(也就是碰撞器范围的坐标点)
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitt = new RaycastHit();
Ray ray =Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hitt);
Debug.Log(hitt.point);
//Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
第二种:
Camera TempC;//定义摄像机变量
private void Start()
{
TempC = this.GetComponent<Camera>();//获取摄像机的组件
Camera
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
Debug.Log(TempC.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, 10.0f)));
}
}
注意:这里容易出现的问题是,从鼠标坐标点转成世界坐标点的时候,新的三维向量Z轴的赋值必须不能是0.0f,因为,屏幕坐 标点的Z轴,其实是相对于当前摄像机的,如果是0,只能转一次世界坐标系,剩下的不管点哪里都是一个值,所以不能把Z值设为和摄像机的Z坐标重合,我们需要设置一个非0的数字,最后转成世界坐标系后的值是,摄像机的Z轴加上所设置的值。例如当前摄像机Z坐标是10f,我所设置的Z值是10f,最后转成世界坐标系后的Z值就是20;下面是老外的经典回复。
Thereare two problems here. The first one is that you need a 'new' in front of yourVector3() in C#. The second is that the 'z' parameter must be the distance infront of the camera. In perspective mode at least, passing 0 will
causeScreenToWorldPoint() to always return the position of the camera. So the callwill look something like:
worldPos
=
Camera.main.ScreenToWorldPoint(new
Vector3(Input.mousePosition.x,Input.mousePosition.y,
10.0f));
Notethe measurement for the 'z' parameter is from camera plane to the playingsurface plane. It is not the distance between the camera and the object. If thecamera is aligned with the axes, then the distance is easy to calculate.
If thecamera is at an angle (i.e. not axes aligned), then ScreenToWorldPoint() is notthe way to go.
来自 <http://answers.unity3d.com/questions/599097/need-help-understanding-screentoworldpoint.html?sort=votes>
这是一种发射线,获取当前点击的物体(具有碰撞器)的坐标点(也就是碰撞器范围的坐标点)
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitt = new RaycastHit();
Ray ray =Camera.main.ScreenPointToRay(Input.mousePosition);
Physics.Raycast(ray, out hitt);
Debug.Log(hitt.point);
//Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition));
}
第二种:
Camera TempC;//定义摄像机变量
private void Start()
{
TempC = this.GetComponent<Camera>();//获取摄像机的组件
Camera
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
Debug.Log(TempC.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, 10.0f)));
}
}
注意:这里容易出现的问题是,从鼠标坐标点转成世界坐标点的时候,新的三维向量Z轴的赋值必须不能是0.0f,因为,屏幕坐 标点的Z轴,其实是相对于当前摄像机的,如果是0,只能转一次世界坐标系,剩下的不管点哪里都是一个值,所以不能把Z值设为和摄像机的Z坐标重合,我们需要设置一个非0的数字,最后转成世界坐标系后的值是,摄像机的Z轴加上所设置的值。例如当前摄像机Z坐标是10f,我所设置的Z值是10f,最后转成世界坐标系后的Z值就是20;下面是老外的经典回复。
Thereare two problems here. The first one is that you need a 'new' in front of yourVector3() in C#. The second is that the 'z' parameter must be the distance infront of the camera. In perspective mode at least, passing 0 will
causeScreenToWorldPoint() to always return the position of the camera. So the callwill look something like:
worldPos
=
Camera.main.ScreenToWorldPoint(new
Vector3(Input.mousePosition.x,Input.mousePosition.y,
10.0f));
Notethe measurement for the 'z' parameter is from camera plane to the playingsurface plane. It is not the distance between the camera and the object. If thecamera is aligned with the axes, then the distance is easy to calculate.
If thecamera is at an angle (i.e. not axes aligned), then ScreenToWorldPoint() is notthe way to go.
来自 <http://answers.unity3d.com/questions/599097/need-help-understanding-screentoworldpoint.html?sort=votes>
相关文章推荐
- 【转】Unity鼠标坐标点转成世界坐标系坐标点
- Unity中的坐标系--屏幕转世界坐标
- opengl 获取鼠标在世界坐标系中的坐标
- unity 鼠标位置转换成世界坐标
- 将Unity中的世界坐标转换成NGUI中的坐标
- 将NGUI的坐标转换Unity中的世界坐标
- Unity 世界坐标转换到NGUI坐标
- Unity 世界坐标转换到NGUI坐标
- MC中使用鼠标的坐标值(xmouse/ymouse)中出现的问题与解决
- Unity UGUI —— 鼠标穿透UI问题(Unity官方的解决方法)
- unity 2d 四大坐标系问题 鼠标点击位置问题 详细入门解析
- Unity 3d 射线(获得鼠标点击的坐标[x,y,z])
- Unity 坐标转换 世界坐标转换到NGUI坐标
- Unity插件之NGUI学习(9)—— Tween和世界坐标系尺寸转换为NGUI尺寸
- Unity插件之NGUI学习(8)—— Table和NGUI尺寸转换为世界坐标系尺寸
- [unity3d]navmesh 自动寻路 鼠标点击的坐标获取 鼠标点击的世界坐标
- [unity基础教程]Unity3D中世界坐标转如何化成屏幕坐标?
- 鼠标单击点在世界坐标系中的射线 计算原理
- Unity 世界坐标 和 NGUI 屏幕坐标间的转换
- Unity插件之NGUI学习(8)—— Table和NGUI尺寸转换为世界坐标系尺寸