- Home /
Having problem with debug.DrawRay
So I'm making a 2D platformer and I was trying to detect collision with the floor with a boolean function that has a Boxcast, it turned out perfect, I also added some debug rays at the coordinates of the boxcast so I could see it, turned out well again. The issue I'm having is that now that I want to recicle my code, so I can detect a wall (just change the values of the Boxcast and the rays), the rays aren't showing (I made shure to have Gizmos on). I dont understand why the first function shows and works, and the second one doesn't. Here's my code:*bc references my BoxCollider2D
//check isGrounded
private bool IsGrounded()
{
float extraDistance = .1f;
RaycastHit2D raycastHit = Physics2D.BoxCast(bc.bounds.center, bc.bounds.size, 0f, Vector2.down, extraDistance, floorLayerMask);
Color rayColor;
if (raycastHit.collider != null)
{
rayColor = Color.green;
}
else
{
rayColor = Color.red;
}
Debug.DrawRay(bc.bounds.center + new Vector3(bc.bounds.extents.x, 0f), Vector2.down * (bc.bounds.extents.y + extraDistance), rayColor);
Debug.DrawRay(bc.bounds.center - new Vector3(bc.bounds.extents.x, 0f), Vector2.down * (bc.bounds.extents.y + extraDistance), rayColor);
Debug.DrawRay(bc.bounds.center + new Vector3(bc.bounds.extents.x, - (bc.bounds.extents.y + extraDistance)), Vector2.left * (bc.bounds.extents.x * 2), rayColor);
return raycastHit.collider != null;
}
//check WallHugging
private bool WalHugging()
{
float extraDistance = .1f;
RaycastHit2D raycastHit = Physics2D.BoxCast(bc.bounds.center, bc.bounds.size, 0f, Vector2.right, extraDistance, floorLayerMask);
Color rayColor;
if (raycastHit.collider != null)
{
rayColor = Color.blue;
}
else
{
rayColor = Color.red;
}
Debug.DrawRay(bc.bounds.center + new Vector3(0f, bc.bounds.extents.y), Vector2.right * (bc.bounds.extents.x + extraDistance), rayColor);
Debug.DrawRay(bc.bounds.center - new Vector3(0f, bc.bounds.extents.y), Vector2.right * (bc.bounds.extents.x + extraDistance), rayColor);
Debug.DrawRay(bc.bounds.center + new Vector3(bc.bounds.extents.x + extraDistance, - bc.bounds.extents.y), Vector2.up * (bc.bounds.extents.y * 2), rayColor);
return raycastHit.collider != null;
}
Answer by Asafmazor_49 · Jun 26, 2020 at 04:34 PM
Hey, I found a solution, it turned out that I needed to reference the method for it to show, I just made a quick reference changing the rigidBody velocity and it shows. Eitherway Thanks @N8- for your answer.
Answer by N-8-D-e-v · Jun 22, 2020 at 05:09 PM
Try using gizmos.drawray, and using that in the OnDrawGizmos() method, here's what I mean https://docs.unity3d.com/ScriptReference/Gizmos.DrawRay.html
Hey, this works, not precisely for what i want to do, but it works. Still, I dont understand why the first function (IsGrounded) displays the rays perfectly, and the next one doesnt :(.
Your answer
Follow this Question
Related Questions
is it possible to use a rotation on a Vector3? 3 Answers
accessing a function? 1 Answer
How do I make a callback run? 1 Answer
Weirdness.. Why does putting yield in function corrupt commands? 1 Answer
problem with calling a function from THIS script, on an object that I find with THIS script. 1 Answer