Hi Community!
I’m somewhat desperate at this point, trying to determine the pixel color of a texture on a rigidbody object using a raycast.
My current approach is to grab the renderer of the object hit by the raycast, access it’s texture via the shared material and use GetPixelBilinear with the textureCoords of the raycast hit to get the specific color at the hitpoint.
In Code:
Physics.Raycast(measurementOrigin, shootVec, out hit, maxDist);
var renderer = hit.collider.gameObject.GetComponent<Renderer>();
var texture = renderer.sharedMaterial.mainTexture as Texture2D;
var textureCoords = hit.textureCoord;
var pixelColor = texture.GetPixelBilinear(textureCoords.x, textureCoords.y);
This seems to be working fine for objects with non-convex meshcolliders. But, if I flick the ‘convex’ switch in the mesh collider panel to ‘on’, RaycastHit.textureCoord allways returns a constant (0,0) vector and things no longer look so good.
However, in the end I would like to be able to add a non-kinematic rigidbody to the object, as I need the object to behave physically correct in the scene when being pushed about by forces. But: if I add the rigidbody to the component with a non-convex mesh collider, Unity3D complains, that rigidobjects require convex colliders. Is there some kind of solution to this conundrum? I.e. is there any way to access the pixel color at the point of a raycast hit on a rigidbody object?