如何基于相机旋转角色? (Roblox)

问题描述 投票:1回答:1

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

我几乎得到了它,但它不是旋转角色模型,而是将屏幕变黑并杀死玩家。我之前在Roblox的RPG中已经看过这个,所以我知道这是可能的,而且可能相当容易。我过去曾经使用过CFrames,所以我不确定为什么我这么难过。

经过几个小时的思考和在线检查后,我想我只是问这个问题以节省时间。实现这一目标的正确方法是什么?

编辑:我的坏,这是我到目前为止。我修好了黑屏,但玩家还是死了。

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个回答
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)

如果您需要任何澄清,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.