What am I doing wrong. I want the object to face the mouse's hit position...
Here's the error " Players.Player1.Backpack.Plane.LocalScript:8: bad argument #3 to 'Angles' (number expected, got no value) " Line 8
local Player = Game.Players.LocalPlayer local Mouse = Player:GetMouse() while true do local person = Player.Character if person ~= nil then local neck = workspace.Head.Neck neck.NECK.C0 = neck.NECK.C0*CFrame.Angles (Mouse.Hit) end wait() end
Please note you have a typo on line 7 of your snippet. It probably should read:
local neck = person.Head.Neck -- as opposed to using `workspace`
In addition, the Neck
weld is actually stored in the Torso, rather than the Head.
and on line 8, you should just use neck.C0
, not neck.NECK.C0
. Snaps have no NECK
property.
The position that a mouse is pointing at is Mouse.Hit.p
.
We are going to inform the neck's weld based on this position.
However, we cannot simply directly use the mouse position. It's relative to the origin (a random space in the world) when we need it to be relative to the resting position of the head.
We know the target looking direction is looking from the position of the head and to the mouse. There's a CFrame constructor for this.
looking = CFrame.new( person.Head.Position , Mouse.Hit.p )
Again, though, this is relative to the origin, not the weld.
ROBLOX's default neck snaps are sort of strange, so we're going to just reset them to make them work nice.
Now we can just use the toObjectSpace
method of CFrames to get what we need for C1
:
neck.C1 = CFrame.new(); neck.C0 = CFrame.new(0, 1.5, 0); -- Reset, because we need the head to be in the -- right place for the ObjectSpace calc neck.C0 = CFrame.new(0, 1.5, 0) * person.Head.CFrame:toObjectSpace( looking )
And that's that!
This will look up and down, too, though. If you don't want that, or maybe you just want to reduce the amount, you can modify looking
.
from = person.Head.Position to = Mouse.Hit.p displacement = to - from displacement = displacement * Vector3.new(1, 0.1 , 1) -- Only 10% up and down as before to = from + displacement looking = CFrame.new(from, to)
I pretty sure Mouse.Hit by itself is an object and not a value so try
neck.NECK.C0 = neck.NECK.C0*CFrame.Angles(Mouse.Hit.p)