Well for a start here's a script that will draw lines around a circle from a single point, turn those lines into raycasts and detect their hit locations and you're on your way. Code (csharp): public class circle : MonoBehaviour { private Vector2 _origin; private Vector2 _circumfrance; public float radius = 8; //size of the circle, if using rays this becomes arbitrary public float lines = 360; // how many lines make up our full circle private int linenumber; private float _degrees; public float interval = .5f; //Setting this to 0 will draw a line every frame, if you want to draw rays you may want to loop through them all in a single frame private float timer; public Material spriteMaterial; //set this to sprite material // Use this for initialization void Start () { _origin = transform.position; _degrees = 360 / lines; linenumber = 1; timer = Time.time - interval; } // Update is called once per frame void Update () { if (Time.time > timer + interval && linenumber <= lines) //after a delay draw the next segment { float t_x = _origin.x + radius * Mathf.Sin((_degrees * linenumber) * Mathf.PI / 180); float t_y = _origin.y + radius * Mathf.Cos((_degrees * linenumber) * Mathf.PI / 180); _circumfrance = new Vector2(t_x, t_y); Debug.Log(_circumfrance.ToString()); DrawLine(_origin, _circumfrance, Color.red); linenumber++; timer = Time.time; } } //this is for creating a line void DrawLine(Vector2 startingPoint, Vector2 endPoint, Color color) { GameObject obj = new GameObject(); obj.name = "Line " + linenumber; LineRenderer line = obj.AddComponent<LineRenderer>(); line.material = spriteMaterial; line.enabled = true; line.sortingLayerName = "UI"; line.sortingOrder = 2; line.positionCount = 2; line.SetPosition(0, startingPoint); line.SetPosition(1, endPoint); line.startWidth = .1f; line.endWidth = .1f; line.useWorldSpace = true; line.startColor = color; line.endColor = color; } } edit: someone posted this in answers, hope it helps http://unitycoder.com/blog/2012/02/15/raycast-realtime-visibility-2-0/
What specifically about light do you need to know? How to make your own lighting model? How to add light to a 2D scene?
You just need to create a diffuse sprite material and attach it to your sprites and simple lights will work.
ok, i got as far as casting rays in all directions and getting the jittery edges, I'm not sure how to go about finding the vertices of colliders, i guess he made them out of line segments with all the vertices defined whereas in unity you'd want it to be compatible with any configuration of colliders. I can get a hit on a polygon collider and convert its vertices into world space but I don't know where they are until I've hit them with a ray anyway and it can only get the vertices of polygon colliders. Neat idea though, if anyone has thought of a solution to this i'd love to know.
You can do an OverlapCircleNonAlloc physics check in the size of the light's radius to find all the colliders in range of the light, then raycast towards the verts.
But wouldn't that still only work on polygon colliders? I could calculate the bounding box of box and edge colliders but what if I hit a circle collider?
Well one option is to use polygon colliders for all your shapes. That really wouldn't be so bad I think. However if it's a circle collider you can still calculate points to cast towards. You have the center and the radius, so you know where the outer-most sides are. You just need to use a direction perpendicular to the light-facing direction to get those outer points relative to the light source.
That would do it, thanks, now the question is should i code it for the sake of coding it considering I really have no use for dynamic lighting in my current project? It would be neat but maybe i'll just hold off until i can think of a project that could use it.
Nothing wrong with doing something just to learn, or for the hell of it. You never know what you'll learn that can help you with solving other problems.
is there a more reliable Iway to see if a point is being blocked than the way i'm doing it? Code (csharp): void findVertices(Vector2 origin, float radius) { RaycastHit2D[] objs = new RaycastHit2D[360]; Physics2D.CircleCastNonAlloc(origin, radius, Vector2.zero, objs); foreach (RaycastHit2D obj in objs) { if (obj) { Vector2[] points = obj.transform.GetComponent<PolygonCollider2D>().points; for(int i = 0; i < points.Length ; i++) { points[i] = obj.transform.TransformPoint(points[i]); RaycastHit2D hit = Physics2D.Linecast(origin, points[i]); if (points[i] == hit.point) { Debug.DrawLine(origin, points[i], Color.red); } } } } } edit: I now realize this doesn't work because if the linecast to the side I see doesn't actually hit it'll return null without worrying about memory allocation for the moment i can kindof fudge it like this Code (csharp): void findVertices(Vector2 origin, float radius) { RaycastHit2D[] objs = Physics2D.CircleCastAll(origin, radius, Vector2.zero); for (int h = 0; h < objs.Length; h++) { Vector2[] points = objs[h].transform.GetComponent<PolygonCollider2D>().points; for (int i = 0; i < points.Length; i++) { points[i] = objs[h].transform.TransformPoint(points[i]); RaycastHit2D[] hits = Physics2D.LinecastAll(origin, points[i]); if (hits.Length == 0 || hits[0].point == points[i]) { Debug.DrawLine(origin, points[i], Color.red); } } } }
Now i'm trying to create and use a sprite by overriding its geometry like so Code (csharp): void UpdatePolygon(GameObject polygon, Vector2[] v, UInt16[] triangles) { float hx = 0, hy = 0; foreach (Vector2 vi in v) { if (vi.x > hx) hx = vi.x; if (vi.y > hy) hy = vi.y; } float lx = hx, ly = hy; foreach (Vector2 vi in v) { if (vi.x < lx) lx = vi.x; if (vi.y < ly) ly = vi.y; } Vector2[] localv = new Vector2[v.Length]; for (int i = 0; i < v.Length; i++) { localv[i] = v[i] - new Vector2(lx, ly); } polygon.GetComponent<SpriteRenderer>().sprite.OverrideGeometry(localv, triangles); polygon.transform.position = (Vector2)transform.InverseTransformPoint(transform.position) + new Vector2(lx, ly); } where I have an input such as and but i'm getting this where the yellow is the generated sprite : I feel like i forgot to multiply something by 3 but i can't think of what...
In that array of verts, you have many (0,0,0) entries in there, but all triangles share that one vert, so there should only be one entry for it at index 0.
Oh, is that it? Facepalm And there we have it: I'm not going to post the code because its a mess and only works on polygon colliders atm but if anyone's attempting something similar I'd be happy to share. https://nkoeppel.itch.io/lighting-demo