Hey, This is my first post so I hope it's in the correct location. I've searched all over the internet and I can't find exactly what I'm looking for or I'm not implementing it correctly. I want to be able to click on objects in the scene and get the color of the pixel in the texture assigned to the object I click in 3D. I don't need to take into account shadows or anything, just the texture info. I can get it to work fine with a cube but I'm having issues with cylinders and spheres. I'm looking for a solution to eventually work on any shape, even prefabs. My Code (essentially copied from other solutions): Code (CSharp): void SomeFunction() { RaycastHit hit; Ray theRay = Camera.main.ScreenPointToRay(Input.mousePosition); if(!Input.GetMouseButtonDown(0)) return; Debug.Log ("Testing"); if(!Physics.Raycast (theRay,out hit)) return; Debug.Log ("A Hit " + hit.transform.position); Renderer rend = hit.transform.GetComponent<Renderer>(); Texture2D tex = rend.material.mainTexture as Texture2D; Vector2 pixelUV = hit.textureCoord; Debug.Log (pixelUV); pixelUV.x *= rend.material.mainTexture.width; pixelUV.y *= rend.material.mainTexture.height; Debug.Log (tex.GetPixel((int)pixelUV.x,(int)pixelUV.y)); } Line 20 - Debug.Log (pixelUV) Always returns (0.0, 0.0) for the Sphere and only works for the top and bottom of the cylinder. My thought is that it has to do with how textures get "wrapped" around the sphere based on each polygon it consists of. I hope it's something simple I'm just missing. Thank you very much
Thanks for the quick response. Yes I have the mesh colliders, forgot to mention that, oops. I didn't change any of the settings in any of them though. The cylinder and sphere's mesh colliders look like the cube which does work.
What do you mean they look like the cube? Sorry that I'm insisting but by default, a Unity Sphere has a sphere collider and a Unity cylinder has a capsule collider. (both of those are not mesh colliders) Also according to the manual: Which is what you're getting, which makes me think you're not using Mesh Colliders.
I've added a mesh collider to all three of them. The cube worked so I set the "mesh colliders" of the cylinder and sphere to look the same. They have nothing added or checked. The only thing the "mesh colliders" have are the "Mesh" category preset to have either "Cube" , "Sphere" or "Cylinder". I guess I should've said I did the same thing to the sphere and cylinder after I got the cube to work properly.
Could you try a mesh you created yourself and see if that works? If everything is using mesh colliders there is no reason at all why a cube should work and a sphere wouldn't.
I've never "created" a mesh before. I've started reading about doing them at run time right now, is this what you are referring to? I'll work on the math while I wait for your response. Thanks for the help.
You are amazing. That totally worked!!!! I'm new to forums in general. Do I need to close a thread and is there a way to give you props officially?