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

How to lerp a table of parts to your mouse position?

Asked by
816i 19
7 years ago
Edited 7 years ago

I have a long strand of parts that for now is linked to your torso and moves according to the song played (audio visualizer thing) but I wanted to change it so instead it moved each block (still in a line) to your mouse

  for i = 1, #Parts2 do
    if i == 1 then
      Parts2[i].CFrame = Parts2[i].CFrame:lerp(game.Players.LocalPlayer.Character.Torso.CFrame+Vector3.new(0,.75,0), 0.9)
    else
      Parts2[i].CFrame = Parts2[i].CFrame:lerp(Parts2[i - 1].CFrame * CFrame.Angles(-loudness / 10, math.sin(-loudness / 1000000000), 0) * CFrame.new(0, 0, 0.15), 0.8)
    end
  end

Thats my current code, if you could help me with having it lerp to my mouse it would be great.

0
The table is made before this 816i 19 — 7y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

You could use the MouseMoved event to detect when the mouse moves, and then group the parts in a model and use the SetPrimaryPartCFrame function to move them.

Note: Remeber to use variables to keep your code clean and organised.

local plr = game.Players.LocalPlayer
local m = plr:GetMouse()
local parts --This is your model of the parts.

repeat wait() until plr.Character

local char = plr.Character
local tor = char:WaitForChild("Torso")
local offset = CFrame.Angles(-loudness / 10, math.sin(-loudness / 1000000000), 0)

function visualize()
    for i = 1, #Parts2 do
        if i == 1 then
            Parts2[i].CFrame = Parts2[i].CFrame:lerp(
                tor.CFrame+Vector3.new(0,.75,0), 
                0.9
            )
        else
            Parts2[i].CFrame = Parts2[i].CFrame:lerp(
                Parts2[i - 1].CFrame * offset * CFrame.new(0, 0, 0.15), 
                0.8
            )
        end
    end
end

m.MouseMoved:Connect(function()
    local spot = m.Hit;
    parts:SetPrimaryPartCFrame(spot)
end)

while wait(.1) do
    visualize()
end
0
I changed that code to that but now the parts dont even show up (they are supposed to be connected to the torso too, I think you've done that, but I had to of these, and I changed one part and not both aren't working even though I changed one 816i 19 — 7y
0
My bad, there are conflicting parts of the code. It's trying to set it to your mouse's position as well as the torso's position. Goulstem 8144 — 7y
0
It needs to be one or the other, can't be both. Goulstem 8144 — 7y
0
Is there a way to have one side linked to the torso and the other following the mouse or is that impossible? 816i 19 — 7y
0
Yeah there's a way.. you could use the CFrame.new(vector origin, vector focus) constructor on the `SetPrimaryPartCFrame` function. Have the origin be the Torso, and the focus be the Mouse. Goulstem 8144 — 7y
Ad

Answer this question