如何通过键盘控制角色上下移动?[Roblox/Lua]
2018-12-2 6:53:47
收藏:0
阅读:282
评论:2
我是 Roblox 编程的新手(虽然我之前已经在 Unity 进行了大量的开发)。基本上,我正在尝试允许用户四处飞行。将重力设为 0,并编写下面的 ControlScript。我想让 Space/Shift 允许人们上下移动。但出现了问题,这似乎并不会修改 y 轴位置。我已经试了将近 4 个小时,但还是找不到问题所在。
提前感谢您的帮助!
-- 声明变量
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local moveVector = Vector3.new(0,0,0)
-- 处理 inputBegan 事件(当玩家刚开始按下一个键时)
userInputService.InputBegan:connect(function(inputObject)
if player.Character then
if inputObject.KeyCode == Enum.KeyCode.W then
moveVector = moveVector + Vector3.new(0,0,-1)
end
if inputObject.KeyCode == Enum.KeyCode.A then
moveVector = moveVector + Vector3.new(-1,0,0)
end
if inputObject.KeyCode == Enum.KeyCode.S then
moveVector = moveVector + Vector3.new(0,0,1)
end
if inputObject.KeyCode == Enum.KeyCode.D then
moveVector = moveVector + Vector3.new(1,0,0)
end
if inputObject.KeyCode == Enum.KeyCode.Space then
moveVector = moveVector + Vector3.new(0,-1,0)
end
if inputObject.KeyCode == Enum.KeyCode.LeftShift then
moveVector = moveVector + Vector3.new(0,1,0)
end
end
end)
-- 处理 inputEnded 事件(当玩家松开一个键时)
userInputService.InputEnded:connect(function(inputObject)
if player.Character then
if inputObject.KeyCode == Enum.KeyCode.W then
moveVector = moveVector + Vector3.new(0,0,1)
end
if inputObject.KeyCode == Enum.KeyCode.A then
moveVector = moveVector + Vector3.new(1,0,0)
end
if inputObject.KeyCode == Enum.KeyCode.S then
moveVector = moveVector + Vector3.new(0,0,-1)
end
if inputObject.KeyCode == Enum.KeyCode.D then
moveVector = moveVector + Vector3.new(-1,0,0)
end
if inputObject.KeyCode == Enum.KeyCode.Space then
moveVector = moveVector + Vector3.new(0,1,0)
end
if inputObject.KeyCode == Enum.KeyCode.LeftShift then
moveVector = moveVector + Vector3.new(0,-1,0)
end
end
end)
-- 在 renderstepped 上更新角色的动作,基于目前正在按下的键
runService.RenderStepped:connect(function()
if player.Character then
player:Move(moveVector, true)
end
end)
点赞
用户4302219
使用 Evan Wrynn 的建议,我已经使用速度让它工作。这是 ControlScript:
-- 声明变量
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local MyBody
local children = workspace:GetChildren()
for i=1,#children do
if children[i].Name == player.Name then
MyBody = children[i].HumanoidRootPart
end
end
local moveVector = Vector3.new(0,0,0)
local velocity = 0
-- 处理 inputBegan 事件(当玩家第一次按下键时)
userInputService.InputBegan:connect(function(inputObject)
if player.Character then
if inputObject.KeyCode == Enum.KeyCode.W then
moveVector = moveVector + Vector3.new(0,0,-1)
end
if inputObject.KeyCode == Enum.KeyCode.A then
moveVector = moveVector + Vector3.new(-1,0,0)
end
if inputObject.KeyCode == Enum.KeyCode.S then
moveVector = moveVector + Vector3.new(0,0,1)
end
if inputObject.KeyCode == Enum.KeyCode.D then
moveVector = moveVector + Vector3.new(1,0,0)
end
if inputObject.KeyCode == Enum.KeyCode.Space then
moveVector = moveVector + Vector3.new(0,-1,0)
速度 = 10
end
if inputObject.KeyCode == Enum.KeyCode.LeftShift then
moveVector = moveVector + Vector3.new(0,1,0)
速度 = -10
end
end
end)
-- 处理 InputEnded 事件(当玩家释放按键时)
userInputService.InputEnded:connect(function(inputObject)
if player.Character then
if inputObject.KeyCode == Enum.KeyCode.W then
moveVector = moveVector + Vector3.new(0,0,1)
end
if inputObject.KeyCode == Enum.KeyCode.A then
moveVector = moveVector + Vector3.new(1,0,0)
end
if inputObject.KeyCode == Enum.KeyCode.S then
moveVector = moveVector + Vector3.new(0,0,-1)
end
if inputObject.KeyCode == Enum.KeyCode.D then
moveVector = moveVector + Vector3.new(-1,0,0)
end
if inputObject.KeyCode == Enum.KeyCode.Space then
moveVector = moveVector + Vector3.new(0,1,0)
速度 = 0
end
if inputObject.KeyCode == Enum.KeyCode.LeftShift then
moveVector = moveVector + Vector3.new(0,-1,0)
速度 = 0
end
end
end)
-- 在 RenderStepped 上更新角色的动作,根据当前按下的键。
runService.RenderStepped:connect(function()
if player.Character then
player:Move(moveVector, true)
MyBody.Velocity = Vector3.new(MyBody.Velocity.X,velocity,MyBody.Velocity.Z)
end
end)
2018-12-02 19:22:15
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

你可以使用键盘和鼠标映射表格来查看哪些按钮对应于特定操作。以下是查看映射表格的方法:
点击屏幕左上角的菜单按钮。
在此屏幕上,你可以进行一些鼠标调整。只要开发者没有为游戏设置相机模式,你就可以在两种不同模式之间切换,并开启/关闭 Shift 锁定功能。另外,你还可以设置你的角色移动模式。关于这些设置的更多信息,
若要查看 Roblox 的鼠标和键盘控制,请