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