如何通过键盘控制角色上下移动?[Roblox/Lua]

我是 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)
点赞
用户10079817
用户10079817

你可以使用键盘和鼠标映射表格来查看哪些按钮对应于特定操作。以下是查看映射表格的方法:

  1. 点击屏幕左上角的菜单按钮。

  2. 在此屏幕上,你可以进行一些鼠标调整。只要开发者没有为游戏设置相机模式,你就可以在两种不同模式之间切换,并开启/关闭 Shift 锁定功能。另外,你还可以设置你的角色移动模式。关于这些设置的更多信息,

  3. 若要查看 Roblox 的鼠标和键盘控制,请

2018-12-02 07:03:41
用户4302219
用户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