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:
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.
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.
Answer by gtrzmbie · Jan 09, 2016 at 01:37 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612055544im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast on mouse position problem. how not to raycast to 0 1 Answer
Strafing diagonally using get.axis in Unity 5? 0 Answers
Quaternion lerp slowing down movement? 1 Answer
Grabbing the Relative eulerAngles.y of a Rotation 1 Answer
Can you make the player do a 180° / u-turn when a button is pressed? 0 Answers