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

How to tween to mouse position?

Asked by 5 years ago

I want to tween a part to the mouse's position and destroy when it gets there. I'm using a script in a tool to perform this task but I don't know how to start or even how to do it. I have the basic knowledge of scripting so can someone please give me an example on how to do that. Thank you in advance.

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

This is how I've done it in the past.

local Mouse = game.Players.LocalPlayer:GetMouse()
Mouse.TargetFilter = workspace.Part
local TweenService = game:GetService("TweenService")

game:GetService("RunService").RenderStepped:Connect(function()
    local Tween = TweenService:Create(workspace.Part, TweenInfo.new(.2, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0), {CFrame = CFrame.new(Mouse.Hit.p)})
    Tween:Play()
end)

(in a localscript)

In this code, we start by setting the mouse's target filter to the part, as to not base the hit position on the part, but rather on what is beneath it. Then you simply create a tween using TweenService, that modifies the parts CFrame to tween to Mouse.Hit.p. You can run this as needed, I've run it on RenderStepped to constantly move it to the mouse's position.

Ad

Answer this question