Raycast with Quaternion directions (C#)

I’m working with a 2D game and am trying to make a Raycast from a specific point to multiple directions, and returns RaycastHit data or where the ray itself ends if it hits nothing. I want to use these points later to create/update a mesh, and I need the shape of the mesh to conform to other colliders so it doesn’t pass through them, but deforms to sort of “cast a shadow.”

So far, I’ve managed to Debug.DrawLine basically what I’m going for, but I’m having some trouble making the code work for rays. For some reason the Unity documentation on Quaternions and rays isn’t really helping that much.

Here’s my code so far:

using UnityEngine;  
using System.Collections;  

public class FlashlightController : MonoBehaviour {  

    private float distance = 3.0f;  
    private int segments = 64;  

    void Update()  
    {  
        if (Input.GetButton ("Fire1"))  
        {  
        RaycastCircle();  
        }  
    }  

    void RaycastCircle()  
    {  
        Vector3 mouseScreenPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);  
        Vector3 mousePos = new Vector3 (mouseScreenPos.x, mouseScreenPos.y, 0);  
        Vector3 targetPos = Vector3.zero;  
        Vector3 endPos;  
        int StartAngle = 0;  
        int finishAngle = 360;  
        int increment = (int)(finishAngle / segments);  

        for (int i = startAngle; i < finishAngle; i += increment)  
        {  

            targetPos = (Quaternion.Euler (0, 0, i) * transform.up).normalized * distance;  
            endPos = new Vector3 (mousePos.x + targetPos.x, mousePos.y + targetPos.y, 0);  

            Debug.DrawLine (mousePos, endPos, Color.red);  
        }  
    }  
}  

Doing this yields results like this:

I have a script to move the player with WSAD and have its rotation around the z-axis follow the mouse pointer on screen, and another script that causes the camera to follow the player as it moves, just in case those are important.

My problem is that I guess I don’t really understand how to replicate that shape with Raycasting. When I try, I only get one ray which goes from the world origin to the mouse position. I believe the format I’d want to use for the ray is something like

if (Physics.Raycast(origin, direction, out RaycastHit, float maxDistance)

but I don’t know how to fill the direction part of that. The documentation just says “put a direction” and then uses transform.forward or transform.up, but I want to iterate through every direction I get from the for loop, and not from the world origin.

So here are my problems:

  1. I don’t understand how to pull just a rotation/direction using a Quaternion to fill the “direction” vector - I know I sort of did it already, but that code is a slightly modified answer from another question, and to be honest I’m still fuzzy on how to use Quaternions.

  2. I can’t get the rays to originate from anywhere besides the world origin.

I’m sorry this is so long, I’m just a tad frustrated at how little I feel I am gleaning from the docs.

If you made it through all of that nonsense, I really appreciate it.

I actually just figured it out. I’m using the Physics2D system to do the Raycasting, and painting hits green and misses red. In case this is something someone else might need some day, here’s the code:

using UnityEngine;
using System.Collections;

public class FlashlightController : MonoBehaviour {

	private float distance = 3.0f;
	private int segments = 64;

	void Update ()
	{
		if (Input.GetButton ("Fire1"))
		{
			RaycastCircle2D ();
		}
	}  

	void RaycastCircle2D()
	{
		Vector2 mouseScreenPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		Vector2 mousePos = new Vector2 (mouseScreenPos.x, mouseScreenPos.y);
		Vector2 targetPos = Vector2.zero;
		Vector2 endPos;
		int startAngle = 0;
		int finishAngle = 360;
		int increment = (int)(finishAngle / segments);

		for (int i = startAngle; i < finishAngle; i += increment)
		{
			Vector2 rayDirection2D = (Quaternion.Euler (0, 0, i) * transform.up).normalized;
			targetPos = rayDirection2D * distance;
			endPos = new Vector2 (mousePos.x + targetPos.x, mousePos.y + targetPos.y);

			RaycastHit2D hit2D = Physics2D.Raycast (mousePos, rayDirection2D, distance);
			if (hit2D.collider != null)
			{
				Debug.DrawLine(mousePos, hit2D.point, Color.green);
			} else {
				Debug.DrawLine (mousePos, endPos, Color.red);
			}
		}
	}
}

So, figured out the Quaternion thing through yet another answer on these forums. I know it’s messy code, but it helps me keep track of what I’m doing while I flounder around.