unity射线ScreenPointToRay以屏幕中点的位置发射射线

第一人称游戏往往有一个准星UI,如果要往准星UI的位置发射一条射线,就可以使用"屏幕位置"来获取准星UI的位置。

如图:

在unity中,左下角的屏幕位置为(0,0),右上角的屏幕位置是(Screen.width,Screen.height)(注意x为宽,y为高)。如图可以了解到,在屏幕位置中,左下角是最小值,右上角为最大值,那么中点自然就是(Screen.width / 2, Screen.height / 2)了。

如果你需要的不是Input.mousePosition鼠标位置,而是屏幕中点,那么这条射线应该这样获取:

m_ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));

ScreenPointToRay()方法:屏幕像素点转换成射线,通常值都是Input.mousePosition,实际上Input.mousePosition获取的也是屏幕点,也就是屏幕位置,而非世界坐标。

详细的世界坐标与屏幕坐标的区别可以看这篇文章:https://blog.csdn.net/yongh701/article/details/71423029

猜你喜欢

转载自blog.csdn.net/weixin_55532142/article/details/121304969
今日推荐