运行环境Win7,Win8,Win10 win64 Reworld版本 体验版
针对零基础读者的补充下载安装 Reworld对应版本 Reworld官网链接:http://www./ 下载后安装后注册账号打开空地图
思路分析以人物实时坐标为中心点来检测周围一定范围的物体,并让物体向人物方向移动,做到一种吸附的效果。 功能效果展示
场景零件搭建一、搭建过程在工作区创建零件并添加服务端脚本
打开服务端脚本,代码如下: local item = script.Parent item.TriggerEnter:Connect(function(player) if player:IsClass("Avatar") then item:Destroy() end end) 代码说明:此代码为零件检测碰撞,碰撞到人物时自行销毁 写好代码后我们在工作区中多复制几个这样的零件
移动的核心功能一、搭建过程在客户端最先加载界面添加客户端脚本
代码如下: wait(2) -- 等待2 local person = Players:GetLocalPlayer() -- 获取本地玩家
function MyUpdate() local pos = person.Avatar.Position --获取玩家实时坐标 local minPoint = pos - Vector3.New(10,10,10) local maxPoint = pos Vector3.New(10,10,10) local ignoreInstance = WorkSpace.新底板 local maxParts = 20 local list = WorkSpace:FindPartsInZone(minPoint,maxPoint,ignoreInstance,maxParts)--检测以玩家为中心的20*20*20的正方体 if #list > 0 then for k,v in pairs(list) do if v then if v:IsClass("Part") then v.Color = Vector3.New(255,0,0) --计算物体和人物的坐标向量并给物体增加速度 v.Velocity = Vector3.New(5*(person.Avatar.Position.x - v.Position.x),5*(person.Avatar.Position.y - v.Position.y) ,5*(person.Avatar.Position.z -v.Position.z)) end end end end end
local function coroutineUpdate()--每隔0.1秒检测 while true do coroutine.wait(0.1) MyUpdate() end end
coroutine.start(coroutineUpdate) 这样一来,简单的吸附功能就做好了,大家来试试吧~ 二、补充说明1.什么是工作区 工作区中的对象是会被可视化显示到3D场景中的,并且只有在工作区中的对象才会发生物理交互。 服务对象 不可创建 不可复制 不可删除 2.什么是服务器脚本? 只会在服务器运行的Lua脚本代码,用于编写服务器逻辑。
3.为什么要使用服务器脚本? 因为碰撞检测只能在服务器脚本使用
4.什么是客户端最先加载?
5.什么是客户端脚本? 只会在客户端执行的脚本,执行的逻辑和表现也只会在本地客户端展现;可在以下几个文件目录下自动执行,客户端脚本在工作区下不会自动执行,需要放在以下对象里面: 1. 客户端最先加载 。
来源:https://www./content-4-422951.html |
|