So I’m trying to make a minimap. Here is how the code below works:
Finds the local player’s position
Finds the rotation of the camera, and sets the man part to have 360 - Camera.Rotation
Finds the vector from the local player to the part
Places the minimap indicator
However, if the part is behind me, the minimap will show it infront of me. If I try and fix this by doing Y = -Y, it does something totally different. Any help would be great please!
function Vec3ToVec2(Vec3)
return Vector2.new(Vec3.X, Vec3.Z - (2 * Vec3.Z))
end
local Location = workspace.Test
function Minimap(MainUI, Status, UIE)
local MiniMap = MainUI:WaitForChild("MiniMap")
local You = MiniMap:WaitForChild("You")
local Squares = {}
local MaxDistance = math.abs(MiniMap.AbsoluteSize.X / 2)
local Scale = 1 -- 1 stud = 1 px
RunService.RenderStepped:Connect(function()
for i,v in pairs(Squares)do
v:Destroy()
end
if not(LocalPlayer.Character and LocalPlayer.Character:FindFirstChildOfClass("Humanoid") and LocalPlayer.Character:FindFirstChild("HumanoidRootPart")) then return end
local YourPosition = Vec3ToVec2(LocalPlayer.Character.HumanoidRootPart.Position)
You.Rotation = 360 - math.deg(math.atan2(Camera.CFrame.lookVector.x, Camera.CFrame.lookVector.z))
for i,v in pairs(Location:GetChildren())do
-- if v.Character and v.Character:FindFirstChild("Humanoid") and v.Character.Humanoid.Health > 0 then
local TheirPosition = v
if TheirPosition then
TheirPosition = Vec3ToVec2(TheirPosition.Position)
local NewVector = YourPosition - TheirPosition
if NewVector.Magnitude <= MaxDistance * Scale then
local Them = Instance.new("ImageLabel")
Them.Parent = You
Them.Position = UDim2.new(0, NewVector.X * Scale, 0, NewVector.Y * Scale)
Them.Size = UDim2.new(0, 10, 0, 10)
Them.Rotation = -You.Rotation
Them.BackgroundColor3 = Color3.new(1, 0, 0)
table.insert(Squares, Them)
end
end
-- end
end
end)
end
I can’t find the direct issue with your code; however I have a few questions and tips that could lead to it:
When checking the magnitude, if you multiply one side by Scale, you should also multiply the other side. Or just leave the multiplication out entirely. MaxDistance is in studs (because you’re multiplying it by scale, which is the basis of my assumption), and NewVector is in studs too, since YourPosition and TheirPosition are directly pulled from the Vector3s.
You could just multiply NewVector by Scale instead of doing it on each of its components.
Set the AnchorPoint for Them to 0.5, 0.5 so it is consistent with the anchor point of the object in workspace you are getting its data from.
How is your minimap setup? For negative values to work properly, the origin point for the frame’s children would need to be in the center of the minimap, meaning their parent frame would need to be positioned to {0.5, 0}, {0.5, 0} inside the actual minimap holder.
Here the red is on the wrong side of the blue. The behaviour also can’t be described as just being flipped on the X axis, it does quite a lot of weird things.
Yes, I understand it can’t be described as “just that” but I have a testing place which works just fine with my adjustments. If necessary, I can provide you with the file.
I’ve attached an example place that shows the minimap functioning correctly. Notice I’ve also commented out where you are rotating “Them” because this will cause the positioning to be improperly applied.