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

How would I use CFrame angles to make my head look at my mouse position?

I've tried once, haven't gotten the hang of it. This is quite demanded by FPS games, such as Battlefield. Here's my amateur attempt at so:

player = game.Players.LocalPlayer
character = player.Character

neck = character.Torso["Neck"]
originalNeckC0 = neck.C0.p
originalNeckC1 = neck.C1.p

local tool = Instance.new("HopperBin",player.Backpack)
tool.Name = "T00l"

function onSelected(mouse)
    print(tool.Name.." has been selected.")
    coroutine.resume(coroutine.create(function()
        while wait() do
            neck.C0 = CFrame.new(originalNeckC0,-mouse.Hit.p)
            neck.C1 = CFrame.new(originalNeckC1,mouse.Hit.p)
        end
    end))
    
end
tool.Selected:connect(onSelected)

It doesn't work. Not sure why. Any help?

2 answers

✓ Accepted answer

Joints work in object-space coordinates. You need to be editing the C1, because the C0 should only be used to mark the joint location.

C0 is the positive "push" away from the center of Part0. This is where the two parts will be joined. C1 is the negative "push" away from C0 where Part1 will be oriented and positioned.

The following is an edited version of your code. All I did was change the area where you edit the C1 property in the coroutine. All you need to do there is get the world-space CFrame where you make the head face a position. Then, transform these coordinates to object-space. Like I said earlier, you need to reference how C1 is calculated to make sure you transform it to the right object-space.

To recap, C1 is in reference to the center of Part0 and then pushed by C0. So, in CFrame, it's

TorsoCFrame * C0

player = game.Players.LocalPlayer
character = player.Character

neck = character.Torso["Neck"]
originalNeckC0 = neck.C0.p
originalNeckC1 = neck.C1.p

local tool = Instance.new("HopperBin",player.Backpack)
tool.Name = "T00l"

function onSelected(mouse)
    print(tool.Name.." has been selected.")
    coroutine.resume(coroutine.create(function()
        while wait() do
            neck.C1 = (CFrame.new(character.Head.Position, mouse.Hit.p)):toObjectSpace(character.Torso.CFrame * neck.C0);
        end
    end))
end

tool.Selected:connect(onSelected)

You'll probably see a bunch of free-model code (and even some mainstream developers) do it using C0, or a combination of both C0 and C1. Don't pay attention to that. They might be able to get by doing it, but that's not the correct way. It's much simpler to just figure out how joints work and then try doing editing with them.

1 comment

0

Thank you!

im not a good scripter but i think this is the problem: you know line 14 in your script

14I while wait() do

i this you should put a number in the brackets because you arent saying how long you need to wait this might not work but im trying to help

1 comment

0

You don't need to put a number inside of the parentheses. wait() has a default time it uses if there is no parameter.

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