- Home /
2d Raycasting, trouble drawing rays at the correct angles
I hate asking for help, but I've been at this for a couple hours last night and today and still running into troubles.. Im trying to work on 2d steering and obstacle avoidance using raycasts. The trouble is the angles of my rays and drawing them. I'm trying to get 3 rays, a center, and a left and right each offset by X degrees (say 20 degrees).. which seems to work fine IF the transform is NOT rotated. As soon as a rotation is applied, my left and right rays always seem to go askew..
This seems like it should be simple, but for some reason I am just struggling here.. and transform.forward always points back behind the scene hence the use of transform.right.. I don't think that should matter, but.. What am I missing (aka doing wrong) & what can I do to CORRECTLY aim and draw my rays in the desired direction even when the transform is rotated?
Heres the entire code in its CURRENT form.. which is revision 9,120,100,203.. or it feels like it.. I appreciate any help!
void Update ()
{
bool centerHit = false;
bool leftHit = false;
bool rightHit = false;
Vector3 lDir = new Vector3(Mathf.Cos(0.35f), Mathf.Sin(0.35f), 0);
Vector3 rDir = new Vector3(Mathf.Cos(-0.35f), Mathf.Sin(-0.35f), 0);
RaycastHit2D hit = Physics2D.Raycast(transform.localPosition, transform.right, 3f, 1 << LayerMask.NameToLayer("waterObstacles"));
RaycastHit2D lHit = Physics2D.Raycast(transform.localPosition, transform.right + lDir, 3f, 1 << LayerMask.NameToLayer("waterObstacles"));
RaycastHit2D rHit = Physics2D.Raycast(transform.localPosition, transform.right + rDir, 3f, 1 << LayerMask.NameToLayer("waterObstacles"));
if (hit)
{
if (hit.transform.tag == "rockObstacle")
{
centerHit = true;
Debug.Log("Center - " + hit.collider);
}
}
if (lHit)
{
if (lHit.transform.tag == "rockObstacle")
{
leftHit = true;
Debug.Log("Left - " + lHit.collider);
}
}
if (rHit)
{
if (rHit.transform.tag == "rockObstacle")
{
rightHit = true;
Debug.Log("Right - " + rHit.collider);
}
}
if (!centerHit)
Debug.DrawRay(transform.localPosition, transform.right * 3f, Color.yellow);
else
Debug.DrawRay(transform.localPosition, transform.right * 3f, Color.red);
if (!leftHit)
Debug.DrawRay(transform.localPosition, transform.right + lDir * 3f, Color.green);
else
Debug.DrawRay(transform.localPosition, transform.right + lDir * 3f, Color.red);
if (!rightHit)
Debug.DrawRay(transform.localPosition, transform.right + rDir * 3f, Color.blue);
else
Debug.DrawRay(transform.localPosition, transform.right + rDir * 3f, Color.red);
}
Sincerely,
currently lost and confuzzed...
Answer by robertbu · Jun 20, 2014 at 04:37 PM
You can calculate your left and right directions by:
Vector3 lDir = Quaternion.AngleAxis( 20.0f, Vector3.forward) * transform.right;
Vector3 rDir = Quaternion.AngleAxis(-20.0f, Vector3.forward) * transform.right;
Then you use these directly (not added) in your Raycast():
RaycastHit2D lHit = Physics2D.Raycast(transform.localPosition, lDir, 3f, 1 << LayerMask.NameToLayer("waterObstacles"));
RaycastHit2D rHit = Physics2D.Raycast(transform.localPosition, rDir, 3f, 1 << LayerMask.NameToLayer("waterObstacles"));
Thank you, I knew I was doing something wrong.. time to read up more on the angleaxis and why I use vector3.forward.. and to move on to making it steer the way I want to now..
Vector3.forward is the axis of rotation, so this caused the vector to be rotated around the 'z' axis. You can multiple a vector by a Quaternion in this order (Q v) but you cannot multiply (v Q)...the compiler will generate an error.