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

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

Asked by 10 years ago

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

Log in to vote
2
Answered by
MrNicNac 855 Moderation Voter
10 years ago

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.

0
Thank you! kayden963 25 — 10y
Ad
Log in to vote
-3
Answered by
istiyak 13
10 years ago

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

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. Minifig77 190 — 9y

Answer this question