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
5 years ago

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

-- Variables --
local replicated = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer
local Remote = game.ReplicatedStorage.RemoteEvents:WaitForChild("Lightning")
local UserInputService = game:GetService("UserInputService")
local humanoid = player.Character:WaitForChild("Humanoid")

Mouse = player:GetMouse()
Debounce = true
wait(1)

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        if Mouse.Position > 500 then end
        else
        player.Character:MoveTo(Mouse.Hit.p)
    end 
end)

1 answer

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

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

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

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

-- Variables --
local replicated = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer
local Remote = game.ReplicatedStorage.RemoteEvents:WaitForChild("Lightning")
local UserInputService = game:GetService("UserInputService")
local humanoid = player.Character:WaitForChild("Humanoid")

Mouse = player:GetMouse()
Debounce = true
wait(1)

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        if (Mouse.Hit.p - humanoid.Parent.HumanoidRootPart.Position).magnitude > 500 then end
        else
        player.Character:MoveTo(Mouse.Hit.p)
    end 
end)

Hope this helps!

Ad

Answer this question