- Home /
Question by
DashReza · May 14, 2019 at 06:38 PM ·
c#reflectionrayraycasthit2dlaser
Problem with reflection using raycasthit2D
HI I'm trying to make a reflection game.I have a problem with reflection,please help me.I get this error : << StackOverflowException UnityEngine.ContactFilter2D.CreateLegacyFilter (System.Int32 layerMask, System.Single minDepth, System.Single maxDepth) >> this is my code :
public int remaining = 5;
public Vector2 firstDirection = new Vector2(1, 1);
private void OnDrawGizmos()
{
reflection(transform.position, firstDirection, remaining);
}
void reflection(Vector2 pos,Vector2 dir,int rest)
{
if (remaining == 0) return;
Vector2 startingPos = pos;
if (Physics2D.Raycast(pos, dir))
{
RaycastHit2D hit = Physics2D.Raycast(pos, dir);
pos = hit.point;
dir = Vector2.Reflect(dir, hit.normal);
}
Gizmos.DrawLine(startingPos, pos);
reflection(pos, dir, remaining - 1);
}
Comment
Your answer
Follow this Question
Related Questions
LineRenderer (Laser Beam) is not following the ray it's going on the wrong direction when reflecting 1 Answer
Shoot Laser 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
What's wrong with my RaycastHit2D? 1 Answer