Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Set Local Motor6d Angle/c0 to Camera Lookvector?

Asked by
A_Mp5 222 Moderation Voter
2 years ago

Hello, I've been making a game. To be specific, the game has Head Turning, which works as it's the generic head-turning script everyone uses: It's not the exact one for privacy reasons, but it uses the same math, basically.

local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local PlayerMouse = Player:GetMouse()

local Camera = workspace.CurrentCamera

local Character = Player.Character or Player.CharacterAdded:Wait()
local Head = Character:WaitForChild("Head")
local Neck = Head:WaitForChild("Neck")

local Torso = Character:WaitForChild("UpperTorso")
local Waist = Torso:WaitForChild("Waist")

local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local NeckOriginC0 = Neck.C0
local WaistOriginC0 = Waist.C0

Neck.MaxVelocity = 1/3

RunService.RenderStepped:Connect(function() 
    local CameraCFrame = Camera.CoordinateFrame

    if Character:FindFirstChild("UpperTorso") and Character:FindFirstChild("Head") then
        local TorsoLookVector = Torso.CFrame.lookVector
        local HeadPosition = Head.CFrame.p

        if Neck and Waist then
            if Camera.CameraSubject:IsDescendantOf(Character) or Camera.CameraSubject:IsDescendantOf(Player) then
                local Point = PlayerMouse.Hit.p

                local Distance = (Head.CFrame.p - Point).magnitude
                local Difference = Head.CFrame.Y - Point.Y

                Neck.C0 = Neck.C0:lerp(NeckOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 1, 0), 0.5 / 2)
                Waist.C0 = Waist.C0:lerp(WaistOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 0.5, 0), 0.5 / 2)
            end
        end
    end 
end)

The issue is, the script only supports head-turning if the character's up vector is facing up, and what counters that and causes an issue is the fact that my game has wall climbing, so when a player is on a wall, the head-turning gets a bit weird.

I don't really understand the math inside of the script, so here's where I need help. I need to figure out a way to set a Motor6d's C0/C1 offset to the player camera look vector angles. I only need the math/code explamation which will simply be to convert the global look vector(camera) to the local (motor6d) offset, and nothing else, I will do the rest of it. Thanks!

Answer this question