Find distance between mouse and target player hrp

I am trying to find the distance between target player and my mouse position any ideas?

3 Likes

Couldn’t you use a hitbox to detect if it gets hit by the sword?

I agree with the user above. I think it would be way more optimal and work a lot smoother to use hitboxes for hit detection.

1 Like

thats not what i mean, i mean that u dont have to click on the player to hit them you only need to click within a range of them, this is to help new players get used to the sword and mobile players because then you dont need to exactly click on them

1 Like

you can check the distance between the player and the attacker using magnitude, and if the distance is within a certain range, deal damage.

So… make the hitbox bigger than the sword. There’s really no reason to track the mouse here.

omd no one gets it, i want to find the distance between the mouse and the player and if they are in a certain range it still hits, a bigger hitbox doesnt change what i want it to do

Just calculate the distance from the player hrp to the attacker’s hrp, there is absolutely no need to find the distance between the mouse and the player.

and im not talking about hitboxes

you dont understand thats not what i want, i want to see how close the player is to the mouse and if they are within a certain range of the mouse it still hits

There are multiple ways to do this, but in this my opinion, this one should suit best:


Create a LocalScript inside the StarterPlayerScripts.

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

mouse.Button1Down:Connect(function()
    mouse.Button1Up:Wait()
    for _,player in pairs(game.Players:GetPlayers()) do
        if player.Character then
        local magnitude = (mouse.Hit.Position-player.Character.HumanoidRootPart.Position).Magnitude
            if magnitude < 5 then
                print("In radius: "..tostring(math.floor(magnitude*10)/10))
            else
                print("Not in radius: "..tostring(math.floor(magnitude*10)/10))
            end
        end
    end
end)

the problem with that is that if u click in the sky its magnitude will be offsetted, i think i got an idea on how to do this ill respond if it works!

You can easily check if a player clicks on the sky, somewhat like this:

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

mouse.Button1Down:Connect(function()
    mouse.Button1Up:Wait()
    if mouse.Target == nil then return end --Check if they're clicking on a part
    for _,player in pairs(game.Players:GetPlayers()) do
        if player.Character then
        local magnitude = (mouse.Hit.Position-player.Character.HumanoidRootPart.Position).Magnitude
            if magnitude < 5 then
                print("In radius: "..tostring(math.floor(magnitude*10)/10))
            else
                print("Not in radius: "..tostring(math.floor(magnitude*10)/10))
            end
        end
    end
end)

I think they want the player to be able to hit even when they click the sky. If I understood correctly what they want, they need to calculate the pixel distance instead of the distance in the 3-dimensional world. For this, they’ll need to calculate the screen point at which the HumanoidRootPart Position is and calculate the distance between that and the screen position of the mouse.

Edit: I forgot that a 3D distance approrpriate for the situation needs to be calculated as well, which Colbert pointed out.

FWIW: something you can do is spherecast or boxcast from the camera to the target. A spherecast mostly to give your raycast “width” since a normal raycast doesn’t have a concept of width. The mouse doesn’t have a concept of depth so the camera would have to proxy for depth regardless of if you choose to derive XY coordinates from the camera or the mouse.

It’s difficult to find out what your use case is without combing through the thread but I understand that what you want to do here is to make it easier for melee hits to land on characters on a different device. Typically the mouse is not a factor in such a method, you would use another point of reference such as the character’s position or a raycast from the player’s camera.

The mouse is a 2D object while world objects are in 3D, so you can’t calculate the distance between either of them without transforming them into an appropriate coordinate. For the mouse (a 2D object), you can give it depth by casting a ray which is how the engine handles things like ClickDetectors. For world objects (like HumanoidRootParts in 3D space), you’d translate them into a point on the screen. With either translation is just some simple vector math.

For game design purposes, I’d strongly recommend against trying to go 3D → 2D plane and then finding the distance on a 2D plane unless you explicitly need that functionality and 3D distance is irrelevant. This would effectively allow mobile players to tap anywhere near characters and deal damage, whereas other devices would be handicapped by a real distance factor between an attacker and a target. Even to accommodate mobile players or make something new-player friendly, you should still put effort into making sure the gameplay experience isn’t hampered by ignoring world distances and purely opting for 2D radii. That would make a combat game incredibly unfun.

1 Like

i tried making it 2d buy idk how to make it a 2d plain to then calculate magnitude

1 Like

The number represents the number of dimensions; 2D means two dimensional and 3D means three dimensional. There’s an X, Y and Z axis; to translate a 3D point into 2D, you only need the X and Z to form the X and Y coordinates of a 2D space, but you’re probably best off using WorldToScreenPoint to derive this.

Magnitude is a scalar property of a vector, so it’s just some simple (vector) math.

I do think the rest of the post is still worth reading but you seem dead set on wanting to specifically translate world coordinates into 2D space, as much as a bad gameplay experience as that will present, so I’ll leave you to that one. Worth doing some research on your own time for a few of these things though and brushing up on some fundamental knowledge such as from the Creator Docs.

i have took a different approach to this, i got the distance between local player and target player then made a raycast and made it use this distance then found the end of the raycast then found distance between the end of the raycast and the target player

1 Like

That’s why I do suggest reading posts in full, since this is one of the suggestions I offered:

:man_shrugging:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.