- Home /
Check if object is lit a.k.a. how to use GetPixel
So I'm creating a game where you can repel stuff with light. Right now I'm using colliders to determine if stuff needs to be repelled. Problem is that light can be emitted over corners, this mean that light can bet cut off, resulting in random repels when they shouldn't be repelled.
I looked up this stuff about Get/Set Pixel but I can't make crystals clear with the information I found. How would one use GetPixel to see if there is a change in brightness on pixels? From the stuff I've seen you can return four values with the last value representing the brightness. But how would you even call and read these values?
Any insight would be significantly appreciated and thanks in advance!
You cannot do what you want with GetPixel(). That is Texture2D.GetPixel() gets the pixel value of a image/texture, but these values do not change due to lighting. Texture values are combined with lighting values in the shader and displayed to the screen. There is a Texture2D.ReadPixels() that reads screen pixels. These values are after the lighting has been applied, but it is hard to map screen pixels back to individual objects. If you have Pro, you might be able to do something with RenderTextures. I know this issue has been written about on UA a time or two in the last year, so consider searching around on UA.
This, ancient post, might help. :)
http://answers.unity3d.com/questions/26099/how-to-detect-if-a-gameobject-is-hit-by-a-light.html