So, we’ve recently been having some problems with some code for our game that we’re creating to display when something is behind you. It works basically fine, however there is a certain sweet spot which causes the x-offset of the ImageLabel/Frame to jolt off to the side. As shown here:
Here’s the code that was used: (If you need any clarification on what some of the names are, let me know)
"markers" is a folder in workspace where the parts are held that are being tracked.
game:GetService("RunService").Heartbeat:Connect(function()
for i, markerData in pairs(markers) do
if not markerData.part:IsDescendantOf(workspace.ActiveItems) then
table.remove(markers, i)
continue
end
local marker = markerData.marker
local markerPart = markerData.part
local markerPos, markerVis = game.Workspace.CurrentCamera:WorldToViewportPoint(markerPart.Position)
local vectorMath = workspace.CurrentCamera.ViewportSize - script.Parent.Holder.AbsoluteSize
local xPos
if markerPos.Z < 0 then
xPos = math.clamp(screen.X - markerPos.X, 0, screen.X - marker.AbsoluteSize.X)
else
xPos = math.clamp(markerPos.X, 0, screen.X - marker.AbsoluteSize.X)
end
local yPos = script.Parent.Holder.AbsoluteSize.Y - marker.AbsoluteSize.Y
marker.Position = UDim2.new(0, xPos, 0, screen.Y - marker.AbsoluteSize.Y)
end
end)
I personally don’t use ImageLabels super often, hence why I’m turning to the forums to find a solution to this problem. Any help would be greatly appreciated, thank you!
This is from basically the exact same problematic distance where the issue is caused.
It tracks it perfectly fine, just freaks out at that distance when it’s behind you, which will be what it’s like in-game. Which of course, is not what we want.
Similar to Mario Kart, it’s meant to show when there’s an item behind you and track the item as it gets closer to you to show where it’s location is behind you. It stays on a consistent y-axis, however the x-axis is what changes as the UI just needs to show you that an item is approaching you. Hopefully this helps!
If you want a great way to indicate if the object is approaching behind you. Try sizing the label it depending on how close it is.
Set the property AnchorPoint to (0.5,0.5) and set a UIAspectRatioContraint in the label and try this.
local Player = game.Players.LocalPlayer
local MaxMagnitudeOfMarker = 40 --Set whatever you want. This is in Studs
local Character = Player.Character or Player.CharacterAdded:Wait()
game:GetService("RunService").Heartbeat:Connect(function()
for i, markerData in pairs(markers) do
if not markerData.part:IsDescendantOf(workspace.ActiveItems) then
table.remove(markers, i)
continue
end
local marker = markerData.marker
local markerPart = markerData.part
local Magnitude = (Character.HumanoidRootPart.Position - markerPart.Position).Magnitude --Returns the difference between the two positions.
if Magnitude <= MaxMagnitudeOfMarker then
marker.Visible = true
local Percent = math.clamp(Magnitude/MaxMagnitudeOfMarker,0,1)
marker.Size = UDim2.fromScale(0.1*Percent,0.1*Percent)
else
mark.Visible = false
end
end
end)
Only thing is that we want it to still be able to move along the bottom of the screen, depending on where the item actually is. This unfortunately didn’t really solve the problem, as we’re just looking for a simple fix to why it jolts off to the sides of the screen when it’s a certain distance away from the part. However, this does help us as we were going to do something similar to this anyways, but we still have the issue of it freaking out and not actually showing the actual location correctly of the part.