0

I'm making a game for which I need a map that shows the player's location and some other icons. I want to scale the 2d map irrespective of my game environment size. How to show the player's location accurately in the 2d map in form of a player's icon. I made a script which goes like this,

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

public class Finalmap : MonoBehaviour
{
    public Transform mapIcon;

    private Vector2 mapOffsetInUnits;
    private float mapScaleFactor;

    public GameObject mapPlane1;
    public GameObject mapPlane2;
    public GameObject player;

    void Start()
    {
        Renderer mapRenderer1 = mapPlane1.GetComponent<Renderer>();
        Vector3 mapSize1 = mapRenderer1.bounds.size;
        float mapWidth1 = mapSize1.x;
        float mapHeight1 = mapSize1.z;

        Renderer mapRenderer2 = mapPlane2.GetComponent<Renderer>();
        Vector3 mapSize2 = mapRenderer2.bounds.size;
        float mapWidth2 = mapSize2.x;
        float mapHeight2 = mapSize2.z;

        mapScaleFactor = Mathf.Min(mapWidth2 / mapWidth1, mapHeight2 / mapHeight1);

        float offsetX = (mapWidth2  - mapWidth1 * mapScaleFactor) / 2f;
        float offsetY = (mapHeight2  - mapHeight1 * mapScaleFactor) / 2f;
        mapOffsetInUnits = new Vector2(offsetX, offsetY);

        Debug.Log("Map offset in units: " + mapOffsetInUnits);

        Debug.Log("Map 1 width: " + mapWidth1 + ", height: " + mapHeight1);
        Debug.Log("Map 2 width: " + mapWidth2 + ", height: " + mapHeight2);
        player = GameObject.FindGameObjectWithTag("Player");
    }
    void Update()
    {
        Vector3 playerPos = player.transform.position;
        Vector3 mapPlane1Pos = mapPlane1.transform.position;
        Quaternion mapPlane1Rot = mapPlane1.transform.rotation;
        Vector3 mapPlane2Pos = mapPlane2.transform.position;
        Quaternion mapPlane2Rot = mapPlane2.transform.rotation;

        float x = ((playerPos.x - mapPlane1Pos.x) * Mathf.Cos(mapPlane1Rot.eulerAngles.y * Mathf.Deg2Rad) + (playerPos.z - mapPlane1Pos.z) * Mathf.Sin(mapPlane1Rot.eulerAngles.y * Mathf.Deg2Rad) - mapOffsetInUnits.x) * mapScaleFactor;
        float y = ((playerPos.z - mapPlane1Pos.z) * Mathf.Cos(mapPlane1Rot.eulerAngles.y * Mathf.Deg2Rad) - (playerPos.x - mapPlane1Pos.x) * Mathf.Sin(mapPlane1Rot.eulerAngles.y * Mathf.Deg2Rad) - mapOffsetInUnits.y) * mapScaleFactor;

        Vector3 iconPos = new Vector3(y, 0f,-x);
        iconPos = Quaternion.Euler(0f, -mapPlane2Rot.eulerAngles.y, 0f) * iconPos + mapPlane2Pos;

        mapIcon.position = iconPos ;
    }
}

When i apply this code the playericon location shows somewhat same but when i scales the map it changes.

I want that when I make the 2d map big and small the player icon needs to stay in the same position, the position when it was before stretching the map.

English is not my first language please do not mind.

derHugo
  • 83,094
  • 9
  • 75
  • 115

1 Answers1

0

You need to recalculate the mapScaleFactor and mapOffsetInUnits every Update to keep it in the right place.

Move part of the code in the Start() function to another function, and call it in every update

void Start()
{
    RecalculateParameters();
    Debug.Log("Map offset in units: " + mapOffsetInUnits);

    player = GameObject.FindGameObjectWithTag("Player");
}

void RecalculateParameters()
{
    Renderer mapRenderer1 = mapPlane1.GetComponent<Renderer>();
    Vector3 mapSize1 = mapRenderer1.bounds.size;
    float mapWidth1 = mapSize1.x;
    float mapHeight1 = mapSize1.z;

    Renderer mapRenderer2 = mapPlane2.GetComponent<Renderer>();
    Vector3 mapSize2 = mapRenderer2.bounds.size;
    float mapWidth2 = mapSize2.x;
    float mapHeight2 = mapSize2.z;

    mapScaleFactor = Mathf.Min(mapWidth2 / mapWidth1, mapHeight2 / mapHeight1);

    float offsetX = (mapWidth2  - mapWidth1 * mapScaleFactor) / 2f;
    float offsetY = (mapHeight2  - mapHeight1 * mapScaleFactor) / 2f;
    mapOffsetInUnits = new Vector2(offsetX, offsetY);

}

void Update()
{
    RecalculateParameters();
    ...
    // your Update function
}

}

Renan Ruan
  • 136
  • 7