Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
1

How do i point the head torwards the mouse's position?

Asked by
Teeter11 260 Moderation Voter
5 years ago

When this code runs there is no error but, I am trying to make the code make the players head stay in the position its at but point to the mouse. What happens is the players head moves to the position of the mouse not facing it.

Tool = script.Parent
Player = game.Players.LocalPlayer
Camera = game.Workspace.CurrentCamera
Mouse = Player:getMouse()
Tool.Equipped:connect(function()
    local weld1 = Instance.new("Weld")
    weld1.Parent = Player.Character.Head
    weld1.Part0 = Player.Character.Head
    weld1.Part1 = Player.Character.Torso    
    while wait() do
        weld1.C0 = CFrame.new(Player.Character.Head.CFrame.p-Mouse.Hit.p)
    end
end)

It would help a lot if you helped! Thanks!

2 answers

Log in to vote
1
Answered by
MrNicNac 855 Moderation Voter
5 years ago

All too much do you see people editing the C0 and setting the Part0 to what I consider "incorrect" things. The Part0 should be the "anchoring" part. For instance, all things should be "anchored" to the torso. Part1 should be the legacy (attachment) part.

C1 is also the value should be edited. Not C0. C0 is the location (relative to Part0's CFrame) of where the two parts will be connected. C1 is the offset from that location.

Tool = script.Parent
Player = game.Players.LocalPlayer
Camera = game.Workspace.CurrentCamera
Mouse = Player:getMouse()

Tool.Equipped:connect(function()
    local weld1 = Instance.new("Weld")
    weld1.Parent = Game.JointsService
    weld1.C1 = Players.Character.Head.CFrame:toObjectSpace(Player.Character.Torso.CFrame)
    weld1.Part0 = Player.Character.Torso
    weld1.Part1 = Player.Character.Head
    while wait() do
        weld1.C1 = CFrame.new(Player.Character.Head.CFrame.p, Mouse.Hit.p):toObjectSpace(Player.Character.Torso.CFrame)
    end
end)
Ad
Log in to vote
0
Answered by 5 years ago

All right, I agree with what the Doc has to say about switching the "Part0" and "Part1". The Torso should always be your anchoring point.

Anyways, I've been the exact same position as you because I tried this same idea. Alright so you know how a weld works correct?

weld.c1 is part1 relative position to part0 but... the cframe your head would be like CFrame.new(0,-1.5,0)yes? When you take the CFrame of the head on line 11 you take its position which maybe Vector3.new(3525,214,1321) so that may be why your head may be displaced.

This is what I figured you should do...

To get the Direction of your head to your mouse you should use this. Vector3 math (;

local dir = (Player.Character.Head.CFrame.p-mouse.Hit.p).unit

Now we have our direction but when you apply it to your script. weld1.C1 = CFrame.new(Vector3.new(0,-1.5,0), Vector3.new(0,-1.5,0) + dir)

Vector3.new(0,-1.5,0) being the relative position of your head

That should just about do it but when I do this, I would do it with a fake head and not a real one so that it is anchored in place.

I wrote this in off the top of my head like how I usually do these things. If I'm wrong just tell me and I'll try to correct myself.

Answer this question