- Home /
Pull Color from pixel of a an invisible texture from Raycast.
I have a silhoutte target that I am trying to change the score depending on how close you make it to the bullseye. I generated a specific texture that I'll refer to as the Hitmap.
I would like to add the hitmap to the target's material so it's not visible to the player.
Next I would like to perform a raycast at the collision point and get the RGBA value so then I can do a comparison to give the player points.
Is this possible in Unity and if so, how should I got making this happen? Thanks in advance!
Answer by FortisVenaliter · Aug 20, 2015 at 09:31 PM
Well, you don't need to apply it to the material. First off, you want to make the hit texture as small as is reasonable, because you'll be looking it up on the CPU and that can be slow. Next, you'll want to make sure it's read/write enabled in the import settings.
When you run the raycast, you'll need to determine the UV coordinates of the point you hit. I think the RaycastHit structure has a field for that, but if not and you're using a quad, it should be pretty easy to calculate from the world position.
With the UVs of the hit determined, you want to convert those to pixel offsets (generally something like [RoundToInt(u*tex.width), RoundToInt(v*tex.height)]) and then call the GetPixel function on the texture. That will return the color, which you can then convert to usable data from the RGB values.