unity实现鼠标拖住3D物体
程序员文章站
2022-05-26 17:26:33
本文实例为大家分享了unity实现鼠标拖住3d物体的具体代码,供大家参考,具体内容如下
把该脚本直接挂在要拖拽的物体上即可
using system.colle...
本文实例为大家分享了unity实现鼠标拖住3d物体的具体代码,供大家参考,具体内容如下
把该脚本直接挂在要拖拽的物体上即可
using system.collections; using system.collections.generic; using unityengine; public class modeldrages : monobehaviour { //发射射线的摄像机 private camera cam; //射线碰撞的物体 private gameobject go; //射线碰撞物体的名字 public static string btnname; private vector3 screenspace; private vector3 offset; private bool isdrage = false; // use this for initialization void start () { cam = camera.main; } // update is called once per frame void update () { //整体初始位置 ray ray = cam.screenpointtoray(input.mouseposition); //从摄像机发出到点击坐标的射线 raycasthit hitinfo; if (isdrage == false) { if(physics .raycast (ray,out hitinfo)) { //划出射线 只有在scene视图中才能看到 debug.drawline(ray.origin, hitinfo.point); go = hitinfo.collider.gameobject; print(btnname); screenspace = cam.worldtoscreenpoint(go.transform.position); offset = go.transform.position - cam.screentoworldpoint(new vector3(input.mouseposition.x, input.mouseposition.y, input.mouseposition.z)); //物体的名字 btnname = go.name; //组件的名字 } else { btnname = null; } } if(input.getmousebutton(0)) { vector3 currentscreenspace = new vector3(input.mouseposition.x, input.mouseposition.y, screenspace.z); vector3 currentposition = cam.screentoworldpoint(currentscreenspace) + offset; if (btnname != null) { go.transform.position = currentposition; } isdrage = true; } else { isdrage = false; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: unity实现屏幕上写字效果