Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Joint CFrame To mouse.hit Both are CFrame values?[Finally solved, figured it out myself :| ]

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

2 answers

✓ Accepted answer

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)

5 comments

0

Sorry, but there's it's labeled as neck and NECK. Its a custom mech with Motor6D's. Neck is the part while NECK is the Motor6d. This is not a Humanoid object. It's a vehicle.

0

Sorry, but there's a reason why it's*

0

BTW, what is "looking" mouse.hit.p?

0

Nvm on the looking, didnt see the variable.

0

Doesnt work. It spazzes the head. And it's not facing in the direction pointed.

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)

5 comments

0

Same Error :c

0

BTW According to "http://wiki.roblox.com/index.php?title=API:Class/Mouse/Hit" Hit is the CFrame value. A position, there's no P value to hit.

0

Maybe I meant Mouse.Target.p

0

Target is an object.

0

Ik but .p is it's position which is a vector3

This discussion is preserved as it was. New answers, comments, and votes are closed.