So I am trying to make radar and I am a bit stuck. This radar is just for players. I want to put all the player characters into a folder in workspace and have the radar display those players. I just can’t seem to figure out how to display them as a blip on the gui.
You don’t need to do this, you can always use this instead:
for _, Player in pairs(game.Players:GetPlayers() ) do
local Character = Player.Character
end
Then you want to get the position of the enemy character relative to yours. If you don’t want the radar to ‘rotate’ with the character (I.e. the top of the radar always represents the same direction), then just can just do this to get the vector from your character to the other players character
local Offset = EnemyCharacter.HumanoidRootPart.Position - MyCharacter.HumanoidRootPart.Position
Then just convert that world position to a position on the radar
local SCALE_FACTOR = 1/100 --Radar will display enemies up to 50 studs away
Offset = Offset * SCALE_FACTOR
Blip.Position = UDim2.new(Offset.X + 0.5, 0, Offset.Z + 0.5, 0)
Just repeat those last two steps for each player, and turn ‘Clip Descendants’ to true for the frame holding the radar blips.
So it makes things a little harder, but we can do that! The simple solution would be to do this:
local Offset = MyCharacter.HumanoidRootPart.CFrame:PointToObjectSpace(EnemyCharacter.HumanoidRootPart.Position)
Now the offset shows the position of the enemy from the perspective of the character. If they are in front of you, it will be on the top of the radar, behind you, on the bottom. The down side is if your characters humanoid root part is not completely vertical, it will distort the radar view. However most of the time there shouldn’t be any reason for it not to be vertical, until you die or are flung around/knocked over.
If this is likely to be problem, I can find a way to fix that but I’ll have to do it tomorrow when I get back
How good are you at trig? This is basically a trig problem, and it is worth understanding how this works.
So the offset as madattak is calculated by doing
local Offset = EnemyCharacter.HumanoidRootPart.Position - MyCharacter.HumanoidRootPart.Position
Okay cool, so we need to get their position relative to us, their distance way + the angle in between
Firstly, before we progress its worth knowing if you don’t already when working with 2D spaces reflecting 3D ones, you should switch the XZ axis out for the YX in a vector2 (i.e (10, 0, 5) becomes (5, 10)
To calculate the rotation relative to us, you do…
math.atan2(offset.y, offset.x) -- in radians
math.atan2 is just tan-1 that you probably studied for trig class. Ask if you want a link to learn about that.
Once we have both rotations and summed them, we need to find the new coordinate. The expression to do this is
local angle = math.atan2(offset.y, offset.x)
local mag = offset.Magnitude
local newOffset = Vector2.new(math.cos(angle) * mag, math.sin(angle) * mag)
then finally we’d add that on to the middle coordinate of our screen. I think you might also need to add on your rotation relative to the center but I’m unsure.
@madattak I tried your version but while the blip moved it was only moving in one corner. @St_pDropRoll I tried your version but it’s not moving at all, it just resets the blip to position 0,0,0,0
local Scale_Factor = 1/25 -- 100 studs away
for i,enemy_player in pairs(game.Players:GetPlayers()) do
--if enemy_player.TeamColor ~= player.TeamColor and enemy_player ~= player then
spawn(function()
local blip = gameUI.Frame.Radar.RadarFrame.Me:Clone()
blip.Name = "Enemy"
blip.Parent = gameUI.Frame.Radar.RadarFrame
blip.BackgroundColor3 = Color3.fromRGB(202, 57, 13)
while wait() do
local enemy_char_pos = ServerAction_ReturnPlayerCharacterPos:InvokeServer(enemy_player)
local offset = enemy_char_pos - player.Character.HumanoidRootPart.Position
local relative_offset = math.atan2(offset.y, offset.x)
local magnitude = offset.Magnitude
local final_offset = Vector2.new(math.cos(relative_offset) * magnitude, math.sin(relative_offset) * magnitude)
--offset = offset * Scale_Factor
blip.Position = UDim2.new(final_offset * Scale_Factor)
end
end)
--end
end