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 6 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

01wait(2)
02local Player = game:GetService("Players").LocalPlayer
03local Part = game.ReplicatedStorage.Part
04local mouse = Player:GetMouse()
05while wait() do
06    local Loc = mouse.Hit
07    print(Loc)
08    Part.Parent = workspace
09    Part.CFrame = Loc+Vector3.new(0,.5,0)
10    Part.Orientation = Vector3.new(0,0,0)
11end

picture of the happening

1 answer

Log in to vote
3
Answered by 6 years ago
Edited 6 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.

01wait(2)
02local Player = game:GetService("Players").LocalPlayer
03local Part = game.ReplicatedStorage.Part
04local mouse = Player:GetMouse()
05mouse.TargetFilter = Part
06while wait() do
07    local Loc = mouse.Hit
08    print(Loc)
09    Part.Parent = workspace
10    Part.CFrame = Loc+Vector3.new(0,.5,0)
11    Part.Orientation = Vector3.new(0,0,0)
12end

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 — 6y
Ad

Answer this question