unity实现鼠标拖住3D物体
- 作者: 做自己ing
- 来源: 51数据库
- 2021-07-08
本文实例为大家分享了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;
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
热点文章
团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
0
使用 MSBuild.exe 在发布模式下构建 C# 解决方案
0
当我发布 Web 应用程序时,AfterPublish 脚本不运行
0
构建时 T4 转换的产品仅在下一个构建中使用
0
ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
0
新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
0
如何将条件编译符号(DefineConstants)传递给 msbuild
0
MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
0
NuGet 包还原找不到包,没有源
0
使用 C# 6.0 功能运行 TFS 构建
0
