How to make clickable minimap?

Hello how to make clickable minimap? That tied to world coordinates, then how to make draw two lines(vertical and horizontal) on minimap position clicked?

are you in godot?

No, im in unity, i juse use icon

The easy way would be to create a blank UI element like raw image and then react to its input according to UI docs. You would then have to convert the coordinates you get automatically from this texture to whatever is your actual minimap coordinate space.

To capture the mouse coordinates you can use in-code EventSystem events such as various IXXHandler interfaces.

You can find some examples here to get you rolling.

Admittedly you don’t need another UI element if your minimap is already in the UI family, you can just write a script directly on top of it, react to input, convert your coordinates, raise some event and you’re good to go. It requires that you have some semi-advanced knowledge on how to hook up UI and work with it programmatically.

How do i do this, im complete noob at programming:
You would then have to convert the coordinates you get automatically from this texture to whatever is your actual minimap coordinate space.

How do i find position clicked between two points?

I think i found a way to find position on minimap click, now how do i convert world position to mouse click position.

I have 4 points, upper and lower on map and upper and lower on minimap. I found mouse clickposition on minimap. How do i make it click on map position now.

Oof, then these are probably hard solutions for you.

In general contemporary game programming you really need to get a solid grip on concepts like linear transformation, vectors, maps … and to do so you need to learn some basic linear algebra, to be able to at least touch the concepts like transforms, matrices, and quaternions that are ubiquitous for all 2D and 3D engines.

You can do many things without knowing what these truly mean, but you’ve already stumbled onto a basic feature that requires at least some command in this domain, and it has very little to do with programming, it’s just pure mathematics, sitting right next to plain geometry.

Things like linear interpolation, local and world space, vectors and trigonometry, this is really bread and butter in gaming, the basic alphabet of everything.

Basically, Unity doesn’t care about your world, it cares only about the local space of your texture, in this particular example. And so the coordinates will likely return in some space adjacent to what UI is used for — either in screen space coordinates (which are 0, 1 bound to screen rectangle) or in pixel domain (according to resolution) or something similar. But imagine your minimap is likely zoomed out and offset (clipped) and what it shows is some other space that is in world units and so on. So you need to be able to take this transition into account, a pixel on the minimap obviously represents something else in your game world, so what you need to do is transform one space into another, and we use matrices for that.

You can easily avoid having to literally use matrices to achieve this, although the point of them is the same. In the context of linear transformation, all you need to account for is 3 things: positional offset, relative rotation and scale. And this is exactly what you have on every object in the component called Transform.

So here are the two approaches you can take to convert a 2D point (a coordinate set with X and Y components)
With matrices

public Vector2 toSpace(Vector2 point, ref Matrix4x4 mtx) {
  return v2(mtx.MultiplyPoint3x4(v3(point)));
}

Where you can create a linear remapping by producing a matrix like so (see TRS)

var mtx = Matrix4x4.TRS(pos: myOffset, q: myRotation, s: myScale);
var result = toSpace(myPoint, ref mtx);

Or without matrices, including a custom approach to 2D
(this leans heavily on my other answer to you in another thread)

// rotation is in radians, for degrees multiply with Mathf.Deg2Rad
static public Vector2 xform(Vector2 point, Vector2 translation, float rotation, Vector2 scale, Vector2 anchor) {
  var polar = new Vector2(cos(rotation), sin(rotation));
  return rotateVector(cmul(scale, points - anchor), polar) + translation + anchor;
  static Vector2 rotateVector(Vector2 v, Vector2 t) => new(t.x * v.x - t.y * v.y, t.y * v.x + t.x * v.y);
}

Observe that the components in this example are applied backwards. First you apply the scale, then rotation, and finally the offset. To get back to the original space (which is important if you want to create a full round-trip, for example, clicking minimap gives you world position, but then if a unit /having a world position/ should generate a blip on the minimap, this works backwards), you’d have to redo everything in the reverse order (or you simply invert the original matrix).

(Above anchor refers to an imaginary point which is the apparent center of rotation/scale. Typically you want this to be in the center of your world/object and likewise it should be expressed in the object space coordinates.)

You can also optimally do a series of points like so

// works with arrays (Vector2[]) or lists (List<Vector2>)
static public void xform(IList<Vector2> points, Vector2 translation, float rotation, Vector2 scale, Vector2 anchor) {
  if(points is null) return;

  var t = polar(rotation);
  var o = anchor + translation;

  for(int i = 0; i < points.Count; i++)
    points[i] = rotateVector(cmul(scale, points[i] - anchor)) + o;

  Vector2 rotateVector(Vector2 v) => new(t.x * v.x - t.y * v.y, t.y * v.x + t.x * v.y);
}

// where polar is
static public Vector2 polar(float rad) => new(cos(rad), sin(rad));

Now which parameters you wish to choose is up to you.

Disclaimer: Haven’t tested this actually, I apologize if it doesn’t work out of the box.

Edit: also fixed some typos

Where do i find cos, sin and cmul values/vars?

public Vector2 toSpace(Vector2 point, ref Matrix4x4 mtx) {
  return v2(mtx.MultiplyPoint3x4(v3(point)));
}

point must be my click on minimap? what is ref for mtx

Isn’t there easier solutions?
I found this

But i get error at hitTransforms.Add(miniMapHit.point);
hitTransforms does not exist in the current context

I really cant understand this code, its so sad, is there easier solutions?

Where do i find points, cos and sin for this script:

static public Vector2 xform(Vector2 point, Vector2 translation, float rotation, Vector2 scale, Vector2 anchor) {
  var polar = new Vector2(cos(rotation), sin(rotation));
  return rotateVector(cmul(scale, points - anchor), polar) + translation + anchor;
  static Vector2 rotateVector(Vector2 v, Vector2 t) => new(t.x * v.x - t.y * v.y, t.y * v.x + t.x * v.y);
}

How do i access this

public static class MyMath {
  public static Vector3 rcp(Vector3 v) => new(1f / v.x, 1f / v.y, 1f / v.z);
  public static Vector3 cmul(Vector3 a, Vector3 b) => new(a.x * b.x, a.y * b.y, a.z * b.z);
  public static Vector3 cdiv(Vector3 a, Vector3 b) => cmul(a, rcp(b));
  public static Vector2 rcp(Vector2 v) => new(1f / v.x, 1f / v.y);
  public static Vector2 cmul(Vector2 a, Vector2 b) => new(a.x * b.x, a.y * b.y);
  public static Vector2 cdiv(Vector2 a, Vector2 b) => cmul(a, rcp(b));
  ...
}

From another script?

I have this

    public Vector3 cmul;
    public Vector3 rcp;
    public Vector3 v;
    private void Start()
    {
        rcp = MyMath.rcp(v);
        cmul = MyMath.cmul(a, rcp(b));
    }

And it says rcp cannot be used like a method.

If it isn’t immediately obvious, then I’m afraid you’ll have a hard time figuring this out, because you need to fill a lot of gaps.

I apologize for omitting these by mistake, cos and sin are equivalent to Mathf.Cos and Mathf.Sin. Though I recommend you use MathF.Cos and MathF.Sin from the System library.

I’ve included the snippet that invokes the library via using clause in that post.
Similarly to how you place using UnityEngine above your MonoBehaviour classes, you’d also include using System;, using System.Collections; and/or using System.Collections.Generic; depending on what you need.

To invoke a static class static method however, you need to fully qualify it (i.e. specify the full proper name of the class). Exactly how Mathf class works.

You can avoid this by doing using static ClassName at the beginning, however the names will start to clash if you’re not careful.

For example

You cannot use rcp as the name of the variable obviously because the name of the variable clashes with the name of the method. Besides, just saying rcp = doesn’t mean anything in C#. Where have you declared rcp?

You obviously need to declare a variable before you attempt to use it.
As I said, too many gaps in knowledge for you to fill.

using UnityEngine;

public MyClass : MonoBehaviour {

    private void Start() {
        var reciprocal = MyMath.rcp(v);
        Debug.Log(MyMath.cmul(a, reciprocal));
    }
}

This is how you make this work (assuming that v and a exist too somewhere in this context, again this is all just an illustration, not a literally copy/pastable code; edit: fixed).

I can only advise you to learn to code in C# and experiment with simpler projects before you’re ready to tackle fully-fledged features like minimaps etc.

Edit: Here’s an alternative approach with using static

using UnityEngine;
using static MyMath;

public MyClass : MonoBehaviour {

    private void Start() {
        var reciprocal = rcp(v);
        Debug.Log(cmul(a, reciprocal));
    }
}

I have public Vector 3 cmul

        cmul = MyMath.cmul(a, reciprocal);

And it says cmul cannot be used like a method.

Refer to the above post. And if you can’t understand these basic constructs in C#, you really should go back to the root of the problem: learning how to code, instead of trying to implement relatively hard features without understanding the very basics of the system.

C# is a separate concern, it has its own syntax, its own rules, a very hefty specification and user manual, from your perspective anything Unity does is practically added on top of it, which means it’s even more complicated and convoluted for a beginner to understand. Sure, some things come predesigned or pre-packaged and have inspectors and other convenient graphical interface to help streamline the authoring part of it, but at the end of the day, if there’s anything custom you’d like, or extensible, or not really useful out of box so you need to tinker with it, boils down to learning C# extensively.

You literally cannot skip this step and call yourself a programmer.

Here are some resources you can check to try and figure these out
extension methods
using keyword
static classes
var keyword

I know im so dumb for coding, but im trying to find working solution for my problem, i really cant understand coding at all. But thanks for help.