WorldToScreenPoint scale problem

Currently I’m working on a hit indicator, which means it will create a 2D small Frame on where a gun’s hit. I use Bullet Holes as a standard, which positioned in the end on a casted ray.

I use WorldToScreenPoint to get the 2D position of the bullet hole such that I can position the small Frame. I also have a crosshair ImageLabel UI that follows the mouse, it’s parented to a Frame.

game:GetService("RunService").RenderStepped:Connect(function()
	frame.Position = UDim2.new(0, mouse.X - script.Parent.AbsoluteSize.X / 2, 0, mouse.Y - script.Parent.AbsoluteSize.Y / 2)
end)

The half transparent part is the Frame, the crosshair is the ImageLabel.
image

I’m trying to put the small frames (mentioned above) inside the crosshair frame. However, the return of WorldToScreenPoint is only the the offset from the bottom left of the screen, let’s say it returned 800 and 400, 2 values representing X and Y respectively, how do I utilize this 2 numbers to relocate the small frame?

I tried to use multiply the values by 0.001 and set them as X and Y’s scale, however since the return is offset but not scale, it didn’t position correctly.
https://gyazo.com/63a568ef9b81b20b65984bdc82ef8c75

					local vector = cam:WorldToScreenPoint(BulletPart.Position)
					indicator.Position = UDim2.new(vector.X ,0, vector.Y, 0)

Do I have to do something with WorldToScreenPoint or locating the Frame?

FYI, I used to put the frames inside a ScreenGui, and their position would be 0, offsetX, 0, offsetY (0,800,0,400 for above example), it did work well since it’s parented inside a ScreenGui but not any Frames, however it’s not located inside the crosshair Frame, which is what I’m trying to achieve right now.

4 Likes

@Headstackk, have you tried to use the other function, CurrentCamera:WorldToViewportPoint, and are the BulletsHole images/decals or MeshParts?

They are 3d parts. What’s the difference between WorldToViewportPoint and WorldToScreenPoint? They should be pretty much the same except one of them account current Gui Inset?

You can subtract the crosshair’s absolute position from the small frames so they are positioned relative to origin.

1 Like

The difference between WorldToViewportPoint and WorldToScreenPoint is that WorldToViewportPoint ignores the guis and WorldToViewportPoint does not ignore them (I don’t know in detail what the difference is and what it changes, just try it).

1 Like

I don’t think my problem is related to it accounts Inset or not, if you watch my gif again, you can see the small frame pattern isn’t offsetting because of the Top Bar, it is in a complete wrong pattern though.

Mean you the red or the black gui? But try the other function, WorldToViewportPoint, else you should modify your script (sorry for the bad English)
Edit2:

local vector = cam:WorldToScreenPoint(BulletPart.Position)
indicator.Position = Vector2.new(vector.X, vector.Y) --Try this

Edit3: Else try this:

--When the Gun fire the Bullets:
local TotalBullet = math.random(15, 25) --The Amount of all Bullets
[[-- local cam = workspace.CurrentCamera --]]
local Table = []
for i = 1, TotalBullet do
   table.insert(Table, i, i)
end

for i,Bullet in pairs(Table) do
    local RandomXRadius = math.random(-indicator.AbsoluteSize.X, indicator.Absolute.X)
    local RandomYRadius = math.random(-indicator.AbsoluteSize.Y, indicator.Absolute.Y)

    local SayOwner = Istance.new("StringValue", Bullet)--Say the Owner of the Bullets
    SayOwner.Value = game.Players.LocalPlayer.Name
    local VeryBullets = Istance.new("Part", workspace)
    VeryBullets.Size = Vector3.new(2.5, 2.5, 2.5)
    VeryBullets.Position = Vector3.new(RandomXRadius, RandomYRadius, 3.5)--Create your Bullets +3.5 Studs in Z Axis 
    local ScreenGui = Istance.new("ScreenGui", game.Players.LocalPlayer.PlayerGui) 
    local Gui = Instance.new("Frame", ScreenGui)
[[--Gui.Position = UDim.new(0, RandomXRadius, 0, RandomYRadius) --You can chose this or:
cam:WorldToScreenPoint(Bullet)
--]]
end --I hope this help you, i write on a IPad, i cant test my script, sorry
1 Like

I think you kinda get me wrong, I had everything being set up, including bullet holes, spread etc.
I’m just having problems on replicating the bullet hole’s position as a UI within the crosshair, which is a frame that follows the mouss.

1 Like

Try the other Functtion, else you should report this Bug or wait that another you help.

Yes I did, they both behave the same except one of them will account the Top Bar Inset and one doesn’t. This is not a bug, and I don’t think my problem is related to the usage of these 2 functions

1 Like

Try with Bullet.CFrame.Position.

The offset is approx 36 pixels on the Y axis, i believe.

When I calculate hits, I typically convert it to scale.

local X, Y = screenGui.AbsoluteSize.X / vector.X, screenGui.AbsoluteSize.Y / vector.Y
indicator.Position = UDim2.new(UDim.new(0, X), UDim.new(0, Y))

Also, I think it has to do with the fact that the parent of the CrosshairFrame is modifying the scale.

2 Likes

Try this:
hitmarker.Position = UDim2.new(0, vector.X - crosshair.AbsolutePosition.X, 0, vector.Y - crosshair.AbsolutePosition.Y)

6 Likes

Much thanks! I just have no clue on converting offset to scale and position it correctly.
https://gyazo.com/5f910a9d04940003a4a6da155a5617a0

how can you be sure that AbsoluteSize are bigger than vector2 , what would happen if vector2 is bigger than AbsoluteSize ? And why AbsoluteSize and not AbsolutePosition ? (Sorry for the Spam, but I still don’t understand)

I think it’s meant to be AbsolutePosition, and I marked the wrong one as solution, Protori should have the right one.

1 Like

You can try also subtracting the X size / 2 from the x position and Y size / 2 from the y position, and as for the scale, you can try getting the magnitude of the distance between the point and the player, although that will be computationally expensive and not really worth the minimal effect.

1 Like

Just a quick FYI the formula for scale to offset is the size of the ui / the position of element.

1 Like

It will make a scale to offset? If yes, What is the formula for make a offset to scale?