0
\$\begingroup\$

Initial Question:

I'm trying to create a minimap for my game in Unity. It consists of a 2D image sprite within a mask. I've got the rotation and the movement within the mask down, but the only problem is the fact that the image rotates around its central point, instead of the central point of the mask, which is where the player is. Is there any way to fix this in code or using parenting or something? Any help is appreciated.

Code for the image:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIMinimap : MonoBehaviour
{
    public Image minimap;
    public Transform border;

    private float xOffset;
    private float yOffset;

    // getting values from the player van
    public CarController van;
    private float yRotation;
    private Vector3 eulerRotation;
    private Quaternion rotation;
    private float yMovement;
    private float xMovement;
    public Vector3 Movement;

    void Start()
    {
        // getting offset which changes with screen size
        xOffset = transform.position.x;
        yOffset = transform.position.y;
    }

    void Update()
    {
        // Getting Y rotation of the van (direction facing)
        yRotation = van.vanRotation.eulerAngles.y;
        // turning that into a vector for rotation of plane (in Z as rotation of map is on a different plane to van)
        eulerRotation = new Vector3(0, 0, yRotation);
        // converting euler angles into quaternion
        rotation.eulerAngles = eulerRotation;
        transform.rotation = rotation ;

        // converting the position of the van on the real map to movement of the image, 0.247... is the scale of the image, while x and
        // y offset account for screensize
        yMovement= (-van.transform.position.x * (float)0.24721213) + (float) + yOffset;
        xMovement = (-van.transform.position.z * (float)0.24721213) + (float) + xOffset;
        Movement = new Vector3(xMovement, yMovement, 0);
        transform.position = Movement;
    }
}
\$\endgroup\$
5
  • 2
    \$\begingroup\$ Did you consider using the transform.RotateAround method to rotate around a point other than the local origin? \$\endgroup\$
    – DMGregory
    Sep 13 at 23:52
  • \$\begingroup\$ That's close to what I want, but that seems to rotate as a function of time instead of matching the rotation of another object as I want, is there another way to do something like that? \$\endgroup\$
    – Sudsy
    Sep 14 at 0:12
  • 1
    \$\begingroup\$ Did you try zeroing your rotation first, so when you rotate around it's not adding to a previous rotation, but always starting from scratch? \$\endgroup\$
    – DMGregory
    Sep 14 at 11:34
  • \$\begingroup\$ Don’t worry about it. I came up with a better idea where I just rotate the marker instead of the whole minimap, which works well and what I should have done in the first place 😂. Thanks you guys for the help. \$\endgroup\$
    – Sudsy
    Sep 15 at 12:10
  • \$\begingroup\$ Want to edit your question to ask "How to show orientation on a minimap" and add your solution as an answer below? \$\endgroup\$
    – DMGregory
    Sep 15 at 13:10

1 Answer 1

0
\$\begingroup\$

The way I did this was to keep a marker in the centre of the minimap mask, and rotate the marker with the player. The movement of the minimap can just be moved with the scale of the map. Example:

Marker:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIMarker : MonoBehaviour
{
    public CarController van;
    public float yRotation;
    private Vector3 eulerRotation;
    private Quaternion rotation;

    // Update is called once per frame
    void Update()
    {
        // Getting Y rotation of the van (direction facing)
        yRotation = van.vanRotation.eulerAngles.y + 90;
        // turning that into a vector for rotation of plane (in Z as rotation of map is on a different plane to van)
        eulerRotation = new Vector3(0, 0, -yRotation);
        // converting euler angles into quaternion
        rotation.eulerAngles = eulerRotation;
        transform.rotation = rotation;
    }
}

Minimap:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIMinimap : MonoBehaviour
{
    public CarController van;
    public float screenHeight;
    public float screenWidth;

    private float xOffset;
    private float yOffset;
    private float yMovement;
    private float xMovement;
    public float yScale;
    public float xScale;
    public Vector2 Movement;

    void Start()
    {
        // getting offset which changes with screen size
        xOffset = transform.position.x;
        yOffset = transform.position.y;
        // declaring the screen width and height
        screenHeight = Screen.height;
        screenWidth = Screen.width;
        // dividing the height/width of a screen by a factor that adjusts for the scale of the map
        yScale = screenHeight / 1675;
        xScale = screenWidth / 3350;
    }

    void FixedUpdate()
    {

        // converting the position of the van on the real map to movement of the image, movement is determined by the scale
        // of the minimap to the actual terrain, while x and y offset change with screen size so must be declared and added
        yMovement= (van.transform.position.x * yScale) + yOffset;
        xMovement = (-van.transform.position.z * xScale) + xOffset;
        Movement = new Vector2(xMovement, yMovement);
        transform.position = Movement;
    }
}

This is definitely not the most elegant solution, but works well enough for my purposes

\$\endgroup\$

You must log in to answer this question.

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