Need help with Color map

Hi everyone.

I am a pretty new beginner in Unity and Need some help. I know there are a lot of threads on this Topic, but I can’t figure out why the given Solutions do not work for me.

I have 2 gameObjects at the same x and z axis representing a map. Below the “normal” map is a colored one. I try to figure out which Region a user clicked by matching the Color to a Color attibute of a ListItem.

My code so far (sorry - it’s in German)

void Update()
{
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.Log("The ray hit at: " + hit.point);
Vector3 hitPoint = hit.point;

Texture2D bild = farbkarte.GetComponent().material.mainTexture as Texture2D;
Color c = bild.GetPixel((int)hitPoint.x, (int)hitPoint.z);
string x = ColorUtility.ToHtmlStringRGB(c);
Debug.Log("Farbe Bild: " + x);
Landschaft l = spiel.LandschaftListe.Find(j => System.Drawing.ColorTranslator.ToHtml(j.farbe) == “#” + x);
if (l != null) Debug.Log(l.name);
}
}
}

The interesting thing for me is: almost every click on my map Displays a “Name” in debug.log. But never the correct one. Even a click on the same Color returns different values. I got the feeling that the return values differ depending on the zoom Level of the main camera.

Additional Information (I do not know if that is important or not)
My gamObjects have the identical scale factor.
The only difference is the mainTexture.
The Color of the textures are set to RGBA32, Alpha transparency of the above image is checked.
The sizes are set to 8192.

Even when lighting or compression could be a problem, that should not explain the different Colors clicking on the same “field”, isn’t it? I think I mixed up some coordinate Systems, but I have no clue what to do and hope for help after 2 days of experimentations.

Thank you.
Greetings from Germany,

Oliver

Sorry for the German Auto correction which randomly puts in Capital letters. Tried to fix as much as I could find.

You need to convert the hit point to be relative to the texture.

So first of all you will need to get it local to the gameobject it hit, but from the bottom left due to where get pixels 0, 0 is.

Vector3 localHitPos = hit.point - hit.collider.gameobject.GetComponent<Renderer>().bounds.min;

I think min is in worlds space, but if its local just add it to the transform.position first.

Now we have the local position we need to get a texture relative position.

Texture2D mainTex = gameobject.GetComponent<Renderer>().material.mainTexture as Texture2D;

Vector3 boundSize = hit.collider.gameobject.GetComponent<Renderer>().bounds.size;

Vector2 hitTexPos = new Vector2();
hitTexPos.x = localHitPos.x * (mainTex.width / boundSize.x);
hitTexPos.y = localHitPos.z * (mainTex.Height / boundSize.z);

hitTexPos.x = Mathf.Round(hitTexPos.x);
hitTexPos.y = Mathf.Round(hitTexPos.y);

Now you can use hitTexPos to grab the pixel, there is a chance that the 0,0 of get pixel is the top left in which case you will need to add boundSize.z to the minValues.z to get the top left.

Thank you very much for this kind and Long explanation, which gives me the chance to learn anything. I’ll try it and give Feedback.

Wow, thank you. You were absolutely right. This solved my Problem.

I have to ask one thing again.
With the same logic I tried to initiate gameObjects at some spots on the map with this Code:

GameObject obj = Instantiate(GameObject.Find("Tower2"), new Vector3(spiel.Städteliste[i].landschaft.lage.X, 0.2f, spiel.Städteliste[i].landschaft.lage.Y) + (landkarte.GetComponent<Renderer>().bounds.min - landkarte.transform.position), Quaternion.Euler(-90, 0, 0));
                obj.transform.parent = landkarte.transform;

The y coordinate and the z coodinate look good, but the x coordinate is far of the expected point. What did I miss? Can you give me another good hint?

Thanks you,

Oliver