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

How do I add a range limit to mouse.hit?

Asked by 2 years ago

I've been trying to make a beam ability and I used an attachment so the player can see where the beam ends and also where the raycast should end, however since the position of the attachment is decided by the Mouse.Hit.Position thing it goes on forever unless there is something in the way. So what I wanted to know is how to add a range limit onto that for example the attachment can't go further than 80 studs away from the humanoid root part. I've tried multiple things and none of them seem to work and the ones that do seem to set a limit on range cause the attachment to go to a completely random location.

0
Use player:DistanceFromCharacter(Mouse.Hit.Position) SuperPuiu 497 — 2y
0
How would I go about using that? erotrixon 0 — 2y

1 answer

Log in to vote
0
Answered by
SuperPuiu 497 Moderation Voter
2 years ago
Edited 2 years ago
local plr = game.Players.LocalPlayer
local Mouse = plr:GetMouse()
local maxDistance = 12 -- In studs

Mouse.Button1Down:Connect(function()
        if plr:DistanceFromCharacter(Mouse.Target.Position) < maxDistance then
                print("Under max distance click!")
        end
end)

Well, this little example shows how to use :DistanceFromCharacter. It returns a number (studs) and it can be used for many things.

Article: Distance From Character

Edit: You can use magnitude as well: Magnitude

0
I see what you mean but what I want to achieve is for the beam to still go even if for example the mouse is pointing at the sky but it stops at a certain distance rather than going on forever erotrixon 0 — 2y
0
Figure out, you might need to use Mouse.Target to detect if an instance is in the way SuperPuiu 497 — 2y
Ad

Answer this question