So i have a targetting system, but it work really weird. (this is not a problem at the start, but when you move away its really hard because you need to get above the player.
Basicly when my mouse is at the target the distance (look in the script) isn’t 0. and when its slightly above, its more closer to zero.
video:
As you can see, when i put my mouse on the target, the distance is 50 (first value) and when i have it above its arount 2? How can i fix this?
Some Extra stuff: (not help needed but apprecieted)
How can i do it so i only target one instead of every single one?
Also how can i optimize this?
Code:
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local part = workspace.Part
local camera = workspace.CurrentCamera
game:GetService("RunService").Stepped:Connect(function()
-- Get the on-screen position of an object
local screenPos, onScreen = camera:WorldToScreenPoint(part.Position)
if onScreen then
-- WorldToScreenPoint gives a Vector3 so we should convert it
local screenPosVector2 = Vector2.new(screenPos.X, screenPos.Y)
-- Get the mouse position
local mousePosition = UserInputService:GetMouseLocation()
-- Compute the distance between the two positions
local distance = (screenPosVector2 - mousePosition).Magnitude
local character = localPlayer.Character.HumanoidRootPart.Position
local PartPos = part.Position
local distanceBetweenCenters = (character - PartPos).magnitude
local MaxDistance = 300 - 0.5 * distanceBetweenCenters
if MaxDistance < 10 then
MaxDistance = 10
end
print(distance,MaxDistance,distanceBetweenCenters)
-- Check that they're close to each other
if distance < MaxDistance then
-- Mouse is close!
part.BrickColor = BrickColor.Green()
else
-- Mouse is not close!
part.BrickColor = BrickColor.Red()
end
end
end)
“So i have a targetting system, but it work really weird. (this is not a problem at the start, but when you move away its really hard because you need to get above the player”
– you explained the issue, but still we need what you are trying to acchieve
and again:
“As you can see, when i put my mouse on the target, the distance is 50 (first value) and when i have it above its arount 2? How can i fix this?”
Alright the mouse is on the target, distance calculated but, what you are trying to get doing that stuff?
that’s why i asked
are you trying to check the [[distance between your Mouse position and the Part Position]]?
Oh, i meant the first one, when i point my mouse at the part, it doesn’t say i point it a the middle, and whn i point it slightly above it is closer, and this is a problem because the targetradius is smaller when you are further away.
This is a really common issue whenever someone tries to convert their cursor position to 3D world. The problem is the topbar GUI inset, which means the Y position is always offset by 36 pixels.
You’re using WorldToScreenPoint, which comes with this little thing:
Do you see the problem? One properly compensates for the GUI inset, while the other one doesn’t. This is the problem because the resolution simply doesn’t match; one will automatically add 36 pixels to the Y dimension while the other one doesn’t.
To fix it, use camera:WorldToViewportPoint instead of camera:WorldtoScreenPoint. ViewportPoint will also ignore the GUI inset which will match the behavior of GetMouseLocation.