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

How to check distance between your player and Mouse position?

Asked by
Neon_N 104
6 years ago

I don't Know what do to with Mouse position so I left the Position checker if Mouse.Position > 500 then end.

01-- Variables --
02local replicated = game:GetService("ReplicatedStorage")
03local player = game:GetService("Players").LocalPlayer
04local Remote = game.ReplicatedStorage.RemoteEvents:WaitForChild("Lightning")
05local UserInputService = game:GetService("UserInputService")
06local humanoid = player.Character:WaitForChild("Humanoid")
07 
08Mouse = player:GetMouse()
09Debounce = true
10wait(1)
11 
12UserInputService.InputBegan:Connect(function(input, gameProcessed)
13    if gameProcessed then return end
14    if input.UserInputType == Enum.UserInputType.MouseButton1 then
15        if Mouse.Position > 500 then end
16        else
17        player.Character:MoveTo(Mouse.Hit.p)
18    end
19end)

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

The answer is quite simple; using the magnitude properties of a Vector3 you can calculate the distance between two positions:

1local distance = (part1.Position - part2.Position).magnitude

Here's your script with the extra snippet of code:

01-- Variables --
02local replicated = game:GetService("ReplicatedStorage")
03local player = game:GetService("Players").LocalPlayer
04local Remote = game.ReplicatedStorage.RemoteEvents:WaitForChild("Lightning")
05local UserInputService = game:GetService("UserInputService")
06local humanoid = player.Character:WaitForChild("Humanoid")
07 
08Mouse = player:GetMouse()
09Debounce = true
10wait(1)
11 
12UserInputService.InputBegan:Connect(function(input, gameProcessed)
13    if gameProcessed then return end
14    if input.UserInputType == Enum.UserInputType.MouseButton1 then
15        if (Mouse.Hit.p - humanoid.Parent.HumanoidRootPart.Position).magnitude > 500 then end
16        else
17        player.Character:MoveTo(Mouse.Hit.p)
18    end
19end)

Hope this helps!

Ad

Answer this question