I have a Doom-like FPS game in Unity, with 3D objects laid out across the map in essentially 2D coordinates. The game scene is 3D with the usual setup where the camera is showing what the player sees.
I'd like to add a "sonar" gadget (like in Alien(s) movies and games), which would show where the surrounding objects on the game map are, relative to the player, and which would rotate so that the "up" direction in the sonar always points to where the camera looks at (i.e. the camera's Y position in this case).
I'm having trouble mapping the camera movements and rotation to the sonar. The sonar has a canvas on which "dots" (sonar "pings") prefabs are instantiated, so the widget contains an accurate representation of in-game objects.
I have an Update()
method on the controller component which does this:
void Update()
{
// This moves the canvas with sonar pings according to camera movements
swivelRectTransform.anchoredPosition = new Vector2(-Camera.main.transform.position.x * RadarScaleFactor, -Camera.main.transform.position.z * RadarScaleFactor);
// This rotates the sonar background and the canvas of sonar pings according to the camera direction
var rotation = new Vector3(0, 180, Camera.main.transform.eulerAngles.y);
swivelRectTransform.localEulerAngles = rotation;
radarBackgroundTransform.localEulerAngles = rotation;
}
This works for rotation as long as the player is at the origin position (0,0) and falls apart otherwise: the canvas always rotates around its pivot, not around the camera's position, and altering the pivot apparently messes up positioning in a way which I don't understand.
Here's a video showing what I have so far: https://www.youtube.com/watch?v=MR15zOnObNk
This seems like it should be a common feature - is there a canonical algorithm which does it correctly?