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

How do i make a part follow my mouse without stacking on itselfe?

Asked by 5 years ago

So im trying to make a part follow the mouse of the player but when i do not move my mouse constantly it goes on itselfe and this repeats until it hits my cam

thats the code i used how can i change that

wait(2)
local Player = game:GetService("Players").LocalPlayer
local Part = game.ReplicatedStorage.Part
local mouse = Player:GetMouse()
while wait() do
    local Loc = mouse.Hit
    print(Loc)
    Part.Parent = workspace
    Part.CFrame = Loc+Vector3.new(0,.5,0)
    Part.Orientation = Vector3.new(0,0,0)
end

picture of the happening

1 answer

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

Hey there! This has to do with Mouse.TargetFilter. Essentially, your mouse is hitting the coordinate point on top of the brick over and over again, which is what causes this effect.

To make this have the behavior you want, you need to ignore the part in question when deciding what coordinate point the mouse is hitting.

You can do this by simply modifying the mouse's TargetFilter.

wait(2)
local Player = game:GetService("Players").LocalPlayer
local Part = game.ReplicatedStorage.Part
local mouse = Player:GetMouse()
mouse.TargetFilter = Part
while wait() do
    local Loc = mouse.Hit
    print(Loc)
    Part.Parent = workspace
    Part.CFrame = Loc+Vector3.new(0,.5,0)
    Part.Orientation = Vector3.new(0,0,0)
end

In this revised code we tell the hit detection to completely ignore this part.

TargetFilter can be a BasePart, Model, or anything of the sort. All descendants of TargetFilter will also be ignored.

1
In the case that you want multiple things ignored, TargetFilted isn't going to do it for ya, if you need this, use a raycast from the camera position, use the Unit from the Mouse.UnitRay, and FindPartsOnRayWithIgnoreList RubenKan 3615 — 5y
Ad

Answer this question