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

Make character face mouse.hit.lookVector at all times?

Asked by 5 years ago
Edited 5 years ago

So I've tried to simply set humanoidRootPart.Orientation = Vector3.new(0, 180, 0) as a test to see if it would keep facing one direction at all times.

That's not what ended up happening at all. The character went spinning all over the place. Just to see what's going on, I set the transparency of the humanoid root part to zero. This is what it looked like. I'm not sure what happened there but it seemed like the character actually separated from the humanoid root part? (although the root part seemed to be always facing one direction)

I've heard that you can use BodyGyro or AlignOrientation, but even after reading the wiki pages of both, I'm stumped on how to use these to change my character's orientation.

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Well, the mouse's lookVector is constantly changing, so just setting the Orientation of the player's HumanoidRootPart will not keep them in sync. Instead, you can try this

First, to check how much the player's mouse will affect movement (higher numbers will make you spin too much), you set _G.MouseSensitivity to 0 (changing this value will make the camera move with the mouse)

Next, we have a few functions to make the mouse move with the camera when not set to 0

Then you will need to check the hit.p of the mouse for the player to move with it (as seen in the Mouse.Move Event)

This LocalScript will move the player to match the mouse, along with some functions to change the camera

_G.MouseSensitivity = 0
_G.MouseSensitivityOffset = 0

local Player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local Mouse = Player:GetMouse()


function RotateCamera(x, y)
    Camera.CoordinateFrame = CFrame.new(Camera.Focus.p) * (Camera.CoordinateFrame - Camera.CoordinateFrame.p) * CFrame.Angles(x, y, 0) * CFrame.new(0, 0, (Camera.CoordinateFrame.p - Camera.Focus.p).magnitude)
end

function GetAngles(cf)
    local lv = cf.lookVector
    return -math.asin(lv.y), math.atan2(lv.x, -lv.z)
end

local LastCF = Camera.CoordinateFrame

function UpdateSensitivity()
    if _G.MouseSensitivity ~= 1 then -- no need to do this if it's 1
        local x, y = GetAngles(LastCF:toObjectSpace(Camera.CoordinateFrame))
        Camera.CoordinateFrame = LastCF
        RotateCamera(x * -(_G.MouseSensitivity + _G.MouseSensitivityOffset), y * -(_G.MouseSensitivity + _G.MouseSensitivityOffset))
        LastCF = Camera.CoordinateFrame
    end
end

Mouse.Move:connect(function()
    UpdateSensitivity()
    local root = Player.Character.HumanoidRootPart
    Player.Character:SetPrimaryPartCFrame(CFrame.new(root.Position, (Mouse.Hit.lookVector*Vector3.new(1,0,1)) + (root.Position*Vector3.new(0, 1, 0))))
end)

Mouse.Idle:connect(function()
    LastCF = Camera.CoordinateFrame
end)

You can change "lookVector" to "p" if you want the player to rotate with the mouse as well I will also note that the "lookVector" doesnt not function properly if the player begins to move, so using "p" is probably the preferred method.

Also, if you don't need to deal with the camera, then only this section is required

_G.MouseSensitivity = 0
_G.MouseSensitivityOffset = 0

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


Mouse.Move:connect(function()
    local root = Player.Character.HumanoidRootPart
    Player.Character:SetPrimaryPartCFrame(CFrame.new(root.Position, (Mouse.Hit.lookVector*Vector3.new(1,0,1)) + (root.Position*Vector3.new(0, 1, 0))))
end)
0
Dont just modify the HRP, use the SerPrimaryPartCFrame function theking48989987 2147 — 5y
0
Modifying the HRP or using the "SetPrimaryPartCFrame" function in the exact same way for this script, but ok, i adjusted it to match SerpentineKing 3885 — 5y
0
that is a myth actually, just modifying the primary part of any model directly produces a different result than modifying the model with SetPrimaryPartCFrame theking48989987 2147 — 5y
0
of course, the results are the same if the model is only made of one part theking48989987 2147 — 5y
View all comments (3 more)
0
lol, well in studio it works fine with the HRP changed, so it obviously isnt broken by doing that. Maybe it's just some property of the game's player though, idk SerpentineKing 3885 — 5y
0
A piece of that final code was perfect. I messed around with it, changing lookVector to p, and putting this in a while loop to make sure it's activated at all times. bloberous 66 — 5y
0
Awesome! Glad I could help. SerpentineKing 3885 — 5y
Ad

Answer this question