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

How to make player face the mouse?

Asked by
Jirozu 71
7 years ago

I have been trying to figure out how to make the player face the mouse, i am currently new to BodyGyro and other things.

local player = game.Players.LocalPlayer
local character = player.Character
local torso = character.LowerTorso
local BodyGyro = Instance.new("BodyGyro", torso)
local bodyGyro = torso.BodyGyro
local mouse = player:GetMouse()


mouse.Move:connect(function()
    bodyGyro.CFrame = CFrame.new(torso.Position, mouse.Hit.p)
end)

4 answers

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago
local player = game.Players.LocalPlayer
local gyro = nil
local mouse = player:GetMouse()
mouse.TargetFilter = game.Workspace.Level
local runService = game:GetService('RunService')
local character = player.Character or player.CharacterAdded:wait()-- use this

local function onCharacterAdded(character)
    local head = character:WaitForChild('Head') -- Use head for R15
    gyro = Instance.new('BodyGyro')
    gyro.P = 50000
    gyro.MaxTorque = Vector3.new(0, 10000, 0)
    gyro.Parent = head

    local humanoid = character:WaitForChild('Humanoid')
    humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
    humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, true)
end

local function isnan(x) return x ~= x end

local function onRenderStep()
    if gyro then
        local mouseHit = mouse.Hit.p
        if not (isnan(mouseHit.X) or isnan(mouseHit.Y) or isnan(mouseHit.Z)) then
            gyro.CFrame = CFrame.new(character.Head.Position, mouseHit)
        end
    end
end

while not player.Character do wait() end
onCharacterAdded(player.Character)
player.CharacterAdded:connect(onCharacterAdded)

runService:BindToRenderStep('TrackMouse', Enum.RenderPriority.Input.Value, onRenderStep)

That's my script for this. Hope this helps :)

0
Doesnt work for me, sorry. Jirozu 71 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

Put the BodyGyro in HumanoidRootPart, also this should be converted into FilteringEnabled

Log in to vote
0
Answered by 3 years ago

RunService = game:GetService('RunService') Players = game:GetService('Players') Player = Players.LocalPlayer Mouse = Player:GetMouse() Char = Player.Character if not Char then Player.CharacterAdded:Wait() Char = Player.Character end

RootPart = Char:WaitForChild('HumanoidRootPart')

RunService.Stepped:connect(function()

local MousePos = Mouse.Hit.p

local lookVector = Vector3.new(MousePos.X,RootPart.CFrame.Y,MousePos.Z)

RootPart.CFrame = CFrame.new(RootPart.CFrame.p,lookVector)

end)

0
there, hope it helps. also put this in StarterPlayer.StarterPlayerScript and this mst be a local script happy_gagarara12 13 — 3y
Log in to vote
-1
Answered by 7 years ago

I think your problem is in local mouse = player:getMouse() as the computer doesn't know what player. If this script is in a starterpack or startergui then you could do script.Parent.Parent:GetMouse() which is what worked for my script

0
No, he's doing it right. Player is a variable he has already. AstrealDev 728 — 7y
0
Maybe it's because im using r15? Jirozu 71 — 7y

Answer this question