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.