0
\$\begingroup\$

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?

\$\endgroup\$
2
  • \$\begingroup\$ Rotate/translate/rotate. The two rotation angles are usually different. \$\endgroup\$
    – user122973
    Commented Feb 14, 2023 at 2:29
  • \$\begingroup\$ By the way, a common way to do a minimap in Unity is with a second camera that looks down on the scene from above and renders to a render texture. When you want objects to appear different on the minimap than in the main view, then you can solve that via layers. Attach a "minimap representation" child game object with a separate sprite or mesh to everything that is supposed to show up on the minimap, put those on a layer "minimap" and have the minimap camera render only that one layer, while the main camera renders everything except the minimap layer. \$\endgroup\$
    – Philipp
    Commented Feb 16, 2023 at 14:48

1 Answer 1

1
\$\begingroup\$

In case anyone else comes across this, here's what to do (intiuited from @Strom's rather cryptic comment):

  1. Reset the rotation of the canvas containing "pings"
  2. Translate the sonar canvas where it needs to be
  3. Apply a new rotation according to the camera bearing and remember it so it can be reset later
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .