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

Custom mouse cursor getting closer to camera and not accurate?

Asked by
KoreanBBQ 301 Moderation Voter
9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

You know how when you set a part's position to mouse.Hit, it's not exactly where you're pointing it at.

My lines are like this, I want a part that moves when your mouse moves.

local pointer=Instance.new("Part")
game:GetService("RunService").RenderStepped:connect(function()
    pointer.Anchored=true
    pointer.Parent=workspace
    pointer.FormFactor="Custom"
    pointer.BrickColor=BrickColor.new("Bright blue")
    pointer.Size=Vector3.new(2,.2,2)
    local player=game.Players.LocalPlayer
    local mouse=player:GetMouse()
        pointer.CFrame=CFrame.new(mouse.Hit.p)
end)

0
Is filtering enabled? How is it not working? EzraNehemiah_TF2 3552 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

I tested your script and it's a quite common problem as far as I've seen. Basically what's happening is the pointer keeps on coming closer and closer to the camera, and there's a rather simple reason why.

What's happening is that the first time you move the mouse, mouse.Hit returns a position on the ground that you were pointing at and the pointer moves to that location. The second time the RenderStepped event fires, mouse.Hit returns a position on the POINTER, instead of the ground. That's happening because the script doesn't know to ignore the pointer. There's a relatively simple solution to this whole mess though.

TargetFilter

The mouse object has a property called TargetFilter which is basically an object for the mouse to ignore. When you set an object as the mouse's TargetFilter, the mouse acts as if that object isn't even there, and just ignores it. You use it like so:

Mouse.TargetFilter = Part

Now that we know how to use the TargetFilter, we can put that in your script, like so:

--I moved all the static code to the top because those only need to be changed once. It's unnecessary to put them in a loop because they will always be the same. Not only that, putting it in a loop could cause unwanted lag
local pointer=Instance.new("Part")
pointer.Anchored=true
pointer.Parent=workspace
pointer.FormFactor="Custom"
pointer.BrickColor=BrickColor.new("Bright blue")
pointer.Size=Vector3.new(2,.2,2)

local player=game.Players.LocalPlayer
local mouse=player:GetMouse()

mouse.TargetFilter = pointer --This makes the mouse ignore the pointer

game:GetService("RunService").RenderStepped:connect(function()
    pointer.CFrame=CFrame.new(mouse.Hit.p) --Only this needs to be in the loop because this is the only thing that changes. All the other code remains the same
end)

Hope this helped!

More info: Mouse

0
Ah thanks didn't know Targetfilter did that. However I found another way, I created a message and put it inside the Character and put the pointer inside it, so its a Local part and it's ignored by the mouse since it's inside the character KoreanBBQ 301 — 9y
Ad

Answer this question