我如何让角色通过键盘向上/向下移动? [Roblox /卢阿]

时间:2018-12-02 06:53:47

标签: lua roblox

我是Roblox脚本的新手(尽管过去我已经做过很多Unity开发),基本上,我试图让用户随处可见。将重力设为0,并在下面编写ControlScript。我想让Space / Shift允许人们上下移动。由于某种原因,这只是不会修改y轴位置。我已经摆弄了大约4个小时,但我找不到问题所在。

预先感谢您的帮助!

-- Declare variables
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local moveVector = Vector3.new(0,0,0)
-- Handle inputBegan event (when player first presses a key)
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)
-- Handle inputEnded event (when player releases a key)
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)
-- On renderstepped update the character's motion based on what keys
-- are currently being pressed
runService.RenderStepped:connect(function()
 if player.Character then
   player:Move(moveVector, true)
 end
end)

2 个答案:

答案 0 :(得分:0)

如果不确定哪个按钮对应给定的操作,则键盘和鼠标映射很有用。要查看此地图:

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

  2. 在此屏幕上,您可以进行一些鼠标调整。只要开发人员尚未设置游戏的摄影机模式,您就可以在两种不同的模式之间切换并打开/关闭Shift Lock。此外,您可以设置头像的移动模式。有关这些设置的更多信息,

  3. 要查看Roblox鼠标和键盘控件,

答案 1 :(得分:0)

按照Evan Wrynn的建议,利用速度进行这项工作。这是ControlScript:

-- Declare variables
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

-- Handle inputBegan event (when player first presses a key)
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)
      velocity = 10
    end
    if inputObject.KeyCode == Enum.KeyCode.LeftShift then
      moveVector = moveVector + Vector3.new(0,1,0)
      velocity = -10
    end
  end
end)

-- Handle inputEnded event (when player releases a key)
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)
      velocity = 0
    end
    if inputObject.KeyCode == Enum.KeyCode.LeftShift then
      moveVector = moveVector + Vector3.new(0,-1,0)
      velocity = 0
    end
  end
end)

-- On renderstepped update the character's motion based on what keys
-- are currently being pressed
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)