Unity Ray 射线检测
2015-06-26 16:29
465 查看
首先在PC端实现鼠标点击某个物体执行某个操作时我们用Input.GetMouseButtonDown(0),获取鼠标点击事件。
再通过Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition)获取鼠标点击屏幕的坐标。
最后判定该坐标是否点击到某个物体:
if(Physics.Raycast(ray,out hit,1000f)
{
if(hit.collider.name==" ")
执行对应操作。
}
具体代码如下:
// 桌面系统鼠标操作
void DesktopInput()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000f))
{
hitname = hit.collider.name;
if (hitname == "moviePlayerCtl")
{
moviePlayer.gameObject.SetActive(true);
maincamer.GetComponent<PosStateRecord>().location("org");
goodsShower.gameObject.SetActive(false);
}
else if (hitname == "showsOnline")
{
maincamer.GetComponent<PosStateRecord>().location("ui");
moviePlayer.gameObject.SetActive(false);
goodsShower.gameObject.SetActive(true);
}
}
}
}
在移动平台操作时,由于没有鼠标,只能用手触屏,所以需要把
if(Input.GetMouseButtonDown(0))改成if(Input.touchCount>0)
触屏坐标的取值方式改成Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position)
移动平台触屏代码如下:
//移动平台触屏操作
void MobileInput()
{
if (Input.touchCount >0)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position );
if (Physics.Raycast(ray, out hit, 1000f))
{
hitname = hit.collider.name;
if (hitname == "moviePlayerCtl")
{
moviePlayer.gameObject.SetActive(true);
maincamer.GetComponent<PosStateRecord>().location("org");
goodsShower.gameObject.SetActive(false);
}
else if (hitname == "showsOnline")
{
maincamer.GetComponent<PosStateRecord>().location("ui");
moviePlayer.gameObject.SetActive(false);
goodsShower.gameObject.SetActive(true);
}
}
}
}
最后,如果想实现多平台性只需要在Update中检测是哪个平台,再对应调用该平台需要的方法即可:
void Update()
{
#if !UNITY_EDITOR && ( UNITY_IOS || UNITY_ANDROID )
MobileInput();
#else
DesktopInput();
#endif
}
再通过Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition)获取鼠标点击屏幕的坐标。
最后判定该坐标是否点击到某个物体:
if(Physics.Raycast(ray,out hit,1000f)
{
if(hit.collider.name==" ")
执行对应操作。
}
具体代码如下:
// 桌面系统鼠标操作
void DesktopInput()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000f))
{
hitname = hit.collider.name;
if (hitname == "moviePlayerCtl")
{
moviePlayer.gameObject.SetActive(true);
maincamer.GetComponent<PosStateRecord>().location("org");
goodsShower.gameObject.SetActive(false);
}
else if (hitname == "showsOnline")
{
maincamer.GetComponent<PosStateRecord>().location("ui");
moviePlayer.gameObject.SetActive(false);
goodsShower.gameObject.SetActive(true);
}
}
}
}
在移动平台操作时,由于没有鼠标,只能用手触屏,所以需要把
if(Input.GetMouseButtonDown(0))改成if(Input.touchCount>0)
触屏坐标的取值方式改成Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position)
移动平台触屏代码如下:
//移动平台触屏操作
void MobileInput()
{
if (Input.touchCount >0)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position );
if (Physics.Raycast(ray, out hit, 1000f))
{
hitname = hit.collider.name;
if (hitname == "moviePlayerCtl")
{
moviePlayer.gameObject.SetActive(true);
maincamer.GetComponent<PosStateRecord>().location("org");
goodsShower.gameObject.SetActive(false);
}
else if (hitname == "showsOnline")
{
maincamer.GetComponent<PosStateRecord>().location("ui");
moviePlayer.gameObject.SetActive(false);
goodsShower.gameObject.SetActive(true);
}
}
}
}
最后,如果想实现多平台性只需要在Update中检测是哪个平台,再对应调用该平台需要的方法即可:
void Update()
{
#if !UNITY_EDITOR && ( UNITY_IOS || UNITY_ANDROID )
MobileInput();
#else
DesktopInput();
#endif
}
相关文章推荐
- Unity3D读取XML文档信息
- Unity3d_UGUI虚拟摇杆(简易)
- Unity3D4.x之AssetBundle学习笔记
- Unity3D研究院之详解游戏开发音频的播放(十五)
- Unity3D-SLua+ProtocolBuffers构建说明
- 追踪所下载的AssetBundle
- Unity3d陀螺仪看全景图
- Unity NGui 利用UIToggle做个分页标签
- Unity NGui 利用UIToggle做个分页标签
- Unity shader 官网文档全方位学习(一)
- Unity3d_NGUI和UGUI的学习
- 关于Unity特效未播放完的问题
- Unity3D实践系列10, Canvas画布的创建和使用
- unity, change shader for me and all my children
- unity3d移动平台性能优化专题16:性能优化实战之20个复杂单位战斗
- Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条(三十一)
- Unity3D加载外部资源
- Unity3D实践系列09, 物理引擎与碰撞检测
- unity5 代理 登录设置
- unity 5 打包apk 手动升级android sdk 5.0 api 21