Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Inst_player = game.Players.LocalPlayer;
- local Inst_mouse = Inst_player:GetMouse();
- repeat wait() until Inst_player.Character;
- local Inst_character = Inst_player.Character;
- local function onMove(Inst_weld)
- --Inst_weld: the weld created when the tool gets equiped
- local Vec_position = Inst_mouse.Hit.p;
- local Vec_globalC0Position = (Inst_weld.Part0.CFrame*Inst_weld.C0).p;
- local CF_pointingCF = CFrame.new(Vec_globalC0Position,Vec_position);
- local CF_pointingCFWithoutPos = CF_pointingCF - CF_pointingCF.p;
- Inst_weld.C0 = CFrame.new(Inst_weld.C0.p)*((Inst_weld.Part0.CFrame-Inst_weld.Part0.CFrame.p):inverse())*CF_pointingCFWithoutPos
- --[[
- Vec_position is where the mouse points to, in the game (ie. 0,3,4)
- Vec_globalC0Position is the global position of the C0 derived by mutliplying the C0 onto the Part0 CFrame and
- taking its position
- CF_pointingCF is a cframe pointing from the the C0's global position to where the mouse pointed
- CF_pointingCFWithoutPos is the position of CF_pointingCF
- ====
- Now, to get a CFrame which makes the arm point to a position is this:
- The >first< thing I tried to do was:
- CFrame.new(Inst_weld.C0.p)*CF_pointingCFWithoutPos (there wasn't the inverse CFrame)
- It did not work because CFrame.new(Inst_weld.C0.p) ALWAYS has a lookvector as if you're facing forwards; so,
- when I moved my character rotation-wise (ie. turn 45 degrees), this line of code spit out a cframe assuming that
- I was facing forwards. So, b/c this is a weld and b/c of that erroneous CFrame, my arm moved more than it should have.
- We needed some way to counteract this rotation and give a proper one. So I thought, if rotating moved it too much,
- then an inverse of the rotation would balance it. I came up with:
- ((Inst_weld.Part0.CFrame-Inst_weld.Part0.CFrame.p):inverse())
- --]]
- end
- script.Parent.Equipped:Connect(function()
- local Inst_weld = Instance.new("Weld");
- Inst_weld.Part0 = Inst_character.Torso;
- Inst_weld.Part1 = Inst_character["Right Arm"];
- Inst_weld.C0 = CFrame.new(1.5,0,0);
- Inst_weld.C1 = CFrame.Angles(math.rad(-90),0,0)*CFrame.new(0,-.5,.5);
- Inst_weld.Parent = Inst_character.Torso;
- Inst_mouse.Move:Connect(function()
- onMove(Inst_weld)
- end)
- --[[
- Analysis:
- First, create weld.
- Set the torso as the Part0 and the Right Arm as the Part1
- Set the C0; this manner makes the arm where it would be without the weld (character's resting state)
- Set the C1; this manner >first< rotates the arm and then moves it.
- Recall that if we set the C1 position first (CFrame.new(0,-.5,1.5) it'll rotate around the offset point, which isn't
- its own position
- Parent the weld as the torso
- When the mouse moves, call onMove with argument as the weld
- --]]
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement