【问题标题】:How would I rotate a character based on their camera? (Roblox)我将如何根据他们的相机旋转角色? (罗布洛克斯)
【发布时间】:2018-07-31 14:32:39
【问题描述】:

在 Roblox 中,您的相机有一个带有 lookVector 等的 CFrame。我想要完成的是检测玩家何时按下了他们的鼠标右键,并通过一个循环,根据相机的 CFrame 旋转他们的角色,直到释放按钮。

我几乎明白了,但不是旋转角色模型,而是让屏幕变黑并杀死玩家。我以前在 Roblox 上的 RPG 中看到过这种做法,所以我知道这是可能的,而且可能相当容易。过去我使用过很多 CFrame,所以我不知道为什么我会遇到这样的困难。

在玩了几个小时的想法和在线检查之后,我想我只是问这个问题来节省时间。实现这一目标的正确方法是什么?

编辑:我的错,这是我到目前为止所拥有的。我修复了黑屏,但播放器仍然死机。

local UIS,Player,Camera,Character,MB2Down = game:GetService('UserInputService'),game.Players.LocalPlayer,workspace.Camera,script.Parent,false
local Torso = Character:FindFirstChild('Torso') or Character:FindFirstChild('UpperTorso')

UIS.InputEnded:Connect(function(Input)
    if Input.UserInputType == Enum.UserInputType.MouseButton2 and MB2Down then
        MB2Down = false
        Character.Humanoid.AutoRotate = true
    end
end)

UIS.InputBegan:connect(function(Input,onGui)
    if Input.UserInputType == Enum.UserInputType.MouseButton2 and not onGui then
        MB2Down = true
        Character.Humanoid.AutoRotate = false
        while MB2Down and wait() do
            Torso.CFrame = CFrame.new(Vector3.new(Torso.CFrame),Vector3.new(Camera.CFrame.p))
        end
    end
end)

【问题讨论】:

  • 你需要发布你的代码。
  • 已更新代码。对不起,我什至没有考虑。

标签: camera rotation roblox


【解决方案1】:

您几乎已经完成了,但让我对我的解决方案采取一种稍微不同的方法。当玩家按下鼠标右键时,让我们将一个函数连接到 Heartbeat 事件,该事件将更新角色的旋转到相机的旋转。此外,我们将旋转 HumanoidRootPart 而不是 Torso/UpperTorso。 HumanoidRootPart 是角色模型的 PrimaryPart,因此如果我们想将模型作为一个整体来操作,我们应该操作的部分。

我们将玩家的旋转锁定到相机的方式如下:

  1. 获取角色的位置。
  2. 获取相机的旋转(通过使用反正切和相机的外观矢量)。
  3. 使用第 1 步和第 2 步中的位置和旋转为玩家构建新的 CFrame。
  4. 将 CFrame 应用于角色的 HumanoidRootPart。

以下代码在 LocalScript 中,位于 StarterCharacterScripts 下:

local userInput = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = script.Parent
local root = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
local camera = workspace.CurrentCamera
local dead = false
local heartbeat = nil


function LockToCamera()
    local pos = root.Position
    local camLv = camera.CFrame.lookVector
    local camRotation = math.atan2(-camLv.X, -camLv.Z)
    root.CFrame = CFrame.new(root.Position) * CFrame.Angles(0, camRotation, 0)
end


userInput.InputEnded:Connect(function(input)
    if (input.UserInputType == Enum.UserInputType.MouseButton2 and heartbeat) then
        heartbeat:Disconnect()
        heartbeat = nil
        humanoid.AutoRotate = true
    end
end)


userInput.InputBegan:Connect(function(input, processed)
    if (processed or dead) then return end
    if (input.UserInputType == Enum.UserInputType.MouseButton2) then
        humanoid.AutoRotate = false
        heartbeat = game:GetService("RunService").Heartbeat:Connect(LockToCamera)
    end
end)


humanoid.Died:Connect(function()
    dead = true
    if (heartbeat) then
        heartbeat:Disconnect()
        heartbeat = nil
    end
end)

如果您需要任何说明,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 2021-12-03
    • 2015-01-08
    • 2020-03-06
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 2021-02-02
    相关资源
    最近更新 更多