Hi, I want to make a script that moves a model to your mouse position. Currently, it is moving it but unfortunately it is coming closer to the camera position over time which is confusing me.
Make sure you Filter the Part with TargetFilter, this will make it so the Mouse will ignore the Part
Mouse.TargetFilter = Part -- Filters Part
The Reason its going towards the Camera is because the Hit Position is on the Part, if you are setting its position to the Hit Position, it will constantly update towards the Camera as it will detect the Hit Position on the Part, TargetFilter will prevent this, making it ignore the Part entirely, so it will touch the Baseplate, not the Part intersecting it.
local Trampoline = nil
local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local CurrentPosition = nil
local Equipped = false
Mouse.TargetFilter = game.Workspace.CurrentCamera
local part = Instance.new("Part", game.Workspace)
part.Anchored = true
part.CanCollide = false
part.Size = Vector3.new(1,1,1)
part.Transparency = 1
task.spawn(function()
Mouse.Move:Connect(function()
if Equipped == true then
CurrentPosition = game.Players.LocalPlayer.Character.Humanoid.TargetPoint
if Trampoline ~= nil then
part.Position = CurrentPosition
Trampoline:SetPrimaryPartCFrame(part.CFrame)
end
end
end)
end)
script.Parent.Equipped:Connect(function()
Equipped = true
if player then
if Trampoline == nil then
local Trampolinez = game.ReplicatedStorage.FakeTramp:Clone()
Trampolinez.Parent = game.Workspace
Trampoline = Trampolinez
for i,v in pairs(Trampolinez:GetChildren()) do
if v:IsA("BasePart") then
Mouse.TargetFilter = v
end
end
end
end
end)
script.Parent.Unequipped:Connect(function()
Equipped = false
if Trampoline ~= nil then
Trampoline:Destroy()
Trampoline = nil
end
end)
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {AllParts:GetChildren()} -- Or whatever
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
local Raycast = workspace:Raycast(LocalPlayer.Character.Humanoid.RootPart.Position, Mouse.Hit, RayParams)
if Raycast then
--blah blah
end
SHOULD WORK I currently cannot test as I am working on a project.
While that will work, the ray will hit parts that it otherwise wouldn’t, since the ray is coming from the character and not the camera. This is code I use in my own game
local MouseRay = workspace.CurrentCamera:ViewportPointToRay(Mouse.X,Mouse.Y,1)|
local RaycastResult = workspace:Raycast(MouseRay.Origin,MouseRay.Direction*5000,Mouse.RaycastParams)|
It’s a module that mimics the mouse object but uses UserInputService and such, so Mouse.X is actually UserInputService:GetMouseLocation().X under the hood
You can still easily get away by using Mouse.TargetFilter, in this case by just setting it to the model. While using raycasting is better because of that, and mouse got deprecated for like a day lol, using mouse is a lot simpler and fine, really.
local mouse = game.Players.LocalPlayer:GetMouse()
local params = RaycastParams.new()
params.FilterDescendantsInstances = {} -- this is the list of things to not interact with
local distance = 1000
local ray = workspace:Raycast(mouse.UnitRay.Origin, mouse.UnitRay.Direction * distance, params)
s local Mouse = game.Players.LocalPlayer:GetMouse() Mouse.Button1Down:connect(function() local Part = Mouse.Target if Part then Part:MoveTo(Mouse.Hit.p) end end)
The problem is that you’re setting your mouse target filter to parts. This means that when you’re moving your mouse over to the model, the mouse is hitting the camera, which is a part.
To fix this, you need to change your mouse target filter.
local Mouse = game.Players.LocalPlayer:GetMouse() Mouse.TargetFilter = workspace – Filters the Workspace Mouse.Button1Down:connect(function() local Part = Mouse.Target if Part then Part:MoveTo(Mouse.Hit.p) end end)
Now, when you click on the part, it’ll move there.
If you’re having trouble with this and want to learn how to make a tool to move parts to the mouse, I recommend you check out my tutorial on it here: