Raycasting 32 rays in a 360 radius (trig ew)

I am trying to cast 32 rays equally around a 360 degree radius. I am terrible with trig and can’t seem to figure out what I’m doing wrong. My code is as follows:

for (float angle = 0; angle <= 360; angle += 11.25)
        {
            int dist = 0;
            double xFloat = GameController.Instance.PlayerInstance.transform.position.x;
            double yFloat = GameController.Instance.PlayerInstance.transform.position.y;

            double xMove = Math.Cos(angle);
            double yMove = Math.Sin(angle);

            while (true)
            {
                xFloat = xFloat + xMove;
                yFloat = yFloat + yMove;
                dist++;

                Debug.DrawRay(GameController.Instance.PlayerInstance.transform.position,
                     GameController.Instance.PlayerInstance.transform.position - new Vector3((float)xFloat, (float)yFloat), Color.green);
                    break;
            }

        }

This is my result:

As you can see the rays are not shooting out on an equal angle. Any ideas? :frowning:

What are you trying to do with this setup of rays? seems excessive IMO.

I am raycasting for a tile based fog of war removing cells that are hit, so you cannot see through walls.

So if you are going to raycast in a circle, why not use a circle collider and use the OnTrigger functions?

Because I need it to not clear cells past walls.

And what’s stopping you from detecting the cell wall with the circle collider, and casting a single ray at it?

Well the walls are multiple cells and you’re pretty much colliding with them anyways. If anything it would save me a couple of rays. Doesn’t let me know why my math is wrong though :frowning:

Considering the length of your rays, 4 to 9 rays against 32 rays is a big enough difference to give your method a pass. The lesser rays you use if at all, the better for performance. You should never need that many rays on a single entity anyway.

You need to convert the angle to radians.
angle *= Mathf.Deg2Rad;

2 Likes

Perfect. Thank you so much :slight_smile: