Hey!
Can anyone tell me how to do a 2D-Flashlight Effect and Day/Night Circle in 2D Games? So all my sprites are painted as they would appear during day and i actually dont want to have day and night sprites.
Changing the Ambient color doesn’t effect any sprite since they are unlit. I assume I have to use some Image Effect there? I know i could use a 50%black spirte tht overlays everything but I am also wondering how to do a Flashlight?
I’m not sure if this would work, but here’s me having a go at how I would go around this problem…
First thought = Shader …
Second thought, Paint a black texture, then rub out a circular part of the texture to make that part transparent when applied to a material in unity ( so the circle part is transparent when applied on plane for example in unity ) This is the kind of texture i mean Imgur: The magic of the Internet ( something like that :L ).
Then apply this to a plane in unity ( quite a large plane what will cover the screen)
Make the plane a child of an empty gameobject, this gameobject should be on the center of your 2D character, and the plane should be positioned on the empty gameobject so that the transparent circle is in the center.
Then you can code this empty gameobject to always follow your character , or even parent it to your character ( i would code it because i can add lerp , but thats your choice ).
When the flashlight / torch is off, you can replace the texture with a completely black one
Sprites are the way to go in a 2D game. they’re made for it. If you want to change how bright or dark your sprites are you can adjust the color any sprite uniformly by changing the color evenly across the RGB channels.
For example… you have a sprite of Super Mario or something that with many colors. By default the color is set to (1f, 1f, 1f, 1f).
if you change that to (0f,0f,0f,1f) it will become completely black. everywhere between 0 and 1 will be a slightly lighter shade, but keeps all his colors and everything, just gets lighter and darker.
you could get the distance from the “light source” and the sprite, convert that to a number between 0 and 1 and set your color to it.
Distance where sprite will be all black = a
Distance where sprite will be fully lit = b
Actual distance between light and sprite = x
number to set the color to = r
solve r:
(x-b)/(a-b) = r … a decimal representing the percentage of distance between the minimum point and the maximum point.
for example:
a = 5
b = 1
x = 3 (half way between full light and no light)
(3-1)/(5-1) = .5 (or 50% of the way between min and max and exactly half lit… so…
this.gameObject.GetComponent<SpriteRenderer>().color = new Color(r, r, r, 1f);