Minimap (Part 2)

OK so to save on everyone’s time, I attempted to create my own minimap (I’m sure mine can use a little more help when I buy more ‘gamey’ graphics for it, but for now it’s all good), but then I also went to the internet to see advanced minimap functionalities, and I found one that converts minimap coordinates to real-world in-game coordinates, which means I can click somewhere on my map, and my player will go there. However, I found the algorithm to provide me coordinates that are off between my minimap, and the game world, and I was curious why. I found this one online:

using System;
using RPG.Movement;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace RPG.Minimap {

    public class MinimapController : MonoBehaviour, IPointerClickHandler {

        [SerializeField] Camera miniMapCamera;
        [SerializeField] GameObject player;
        
        private void Awake() {
            player = GameObject.FindWithTag("Player");
        }

        public void OnPointerClick(PointerEventData eventData) {

            Vector2 cursor = new Vector2(0,0);

            if (RectTransformUtility.ScreenPointToLocalPointInRectangle(GetComponent<RawImage>().rectTransform,
            eventData.pressPosition, eventData.pressEventCamera, out cursor)) {

                Texture texture = GetComponent<RawImage>().texture;
                Rect rect = GetComponent<RawImage>().rectTransform.rect;

                float coordX = Mathf.Clamp(0, (((cursor.x - rect.x) * texture.width) / rect.width), texture.width);
                float coordY = Mathf.Clamp(0, (((cursor.y - rect.y) * texture.height) / rect.height), texture.height);

                float calX = coordX/texture.width;
                float calY = coordY / texture.height;

                cursor = new Vector2(calX, calY);
                CastRayToWorld(cursor);

            }

        }

        private void CastRayToWorld(Vector2 vec) {

            Ray mapRay = miniMapCamera.ScreenPointToRay(new Vector2(vec.x * miniMapCamera.pixelWidth,
            vec.y * miniMapCamera.pixelHeight));

            RaycastHit miniMapHit;

            if (Physics.Raycast(mapRay, out miniMapHit, Mathf.Infinity)) {

                Debug.Log("miniMapHit: " + miniMapHit.collider.gameObject);
                player.GetComponent<Mover>().MoveTo(miniMapHit.collider.gameObject.transform.position, 1.0f);
                

            }

        }

    }

}

So… why would my minimap lead me to somewhere off in the game coordinates? This script is properly setup, and the function itself works fine. My only problem is, the coordinates that the map outputs do not match the position I want my player to go to in the game world, so this script might just need a little bit of help :slight_smile:

This line is moving the player to the origin of the collider it hit, not where the ray hit the collider. Use hit.point instead

player.GetComponent<Mover>().MoveTo(miniMapHit.point, 1.0f);

See the difference:

hey man, it’s good to see you again. Apologies, I just saw this. For some reason, your solution didn’t fully work either, as my map still sends me a million miles away from the location I clicked on, in the real-world map (I mean no offence. I will return to this function soon)… :sweat_smile:

I haven’t done much in the way of clicking on a minimap, so I’m honestly not sure how much help I can be on this one… My first thought was that ScreenPointToRay straight from the camera and mouse position should do it, but of course, that would not be the case. It does need to be translated into the map camera’s actual coordinates, through the RawImage…

That being said, I’m at a spot where I can tell the calculation to get the relative cursor location is flawed, but I’m not sure how to get there… I’ll try to take a look at this tomorrow and see what I can come up with.

1 Like

I almost abandoned this one tbh, doesn’t sound like a deal breaker (although it could be a very nice add on tbh), especially that the new death item duplication showed up in my game (link is Death Item Drop Bug ), and I have a lot of other concepts (probably not as much as what we covered so far) to go through first

By all means I’m happy to work on this one with you guys :slight_smile:

Privacy & Terms