- Home /
Laser Render Reflect Angle incorrect
I'm shooting a linerender beam out of the foward position of a gameobject and if it hits any object with the tag "Mirror" I want it to bounce off at the correct angle. However, there's something wrong with my laser reflect code, and I'm inexperienced in this aspect of Unity. For some reason, when the laser hits an object with a 45 degree angle on it, it seems to pass through the mirror object and shoot off at a larger angle. View my code below:
void RenderLaser(){
laserLine.SetColors (color, color);
laserLine.SetPosition (0, transform.position);
laserRay.origin = transform.position;
laserRay.direction = transform.forward;
if (Physics.Raycast (laserRay, out laserHit, range)) {
laserLine.SetPosition (hitCount, laserHit.point);
if(laserHit.collider.tag == "Mirror"){
hitCount += 1;
laserLine.SetVertexCount(hitCount+2);
laserLine.SetPosition (hitCount, laserHit.point);
Debug.Log(transform.position);
Debug.Log(laserRay.origin);
Debug.Log(laserHit.normal);
Debug.Log(laserHit.point);
Vector3 reflectPos = Vector3.Reflect(transform.position, laserHit.normal);
hitCount += 1;
laserLine.SetPosition(hitCount, reflectPos);
}
} else {
laserLine.SetPosition (hitCount, laserRay.origin + laserRay.direction * range);
return;
}
}
Thanks in advance!
Answer by hexagonius · Feb 11, 2015 at 08:53 PM
The calculation of the reflectPos is not correct. Vector3.Reflect returns a direction, not a position.
try this
Vector3 reflectPos = Vector3.Reflect(laserRay.direction, laserHit.normal);
laserLine.SetPosition(hitCount, laserHit.point + reflectPos);
I'm to lazy to check if reflectPos is normalized and of the length you want though :)
hey nice! The reflection went at the correct angle but for some reason the initial laserLine end-position width is zero and the "bounced" laserLine width remains a constant .075 (I set this on the component) but only travels 1 unit from the "bounce" origin. Trying to debug with this code:
Vector3 reflectPos = Vector3.Reflect(laserRay.direction, laserHit.normal);
Debug.Log (reflectPos);
Debug.Log (laserHit.point);
hitCount += 1;
laserLine.SetPosition(hitCount, laserHit.point + reflectPos);
Debug.Log (laserHit.point + reflectPos);
In this case:
reflectPos = (-1,0,0)
laserHit.point = (-3,1,0)
laserHit.point + reflectPos = (-4,1,0)
I understand what is happening, but I'm not sure how to solve it. It's setting the endpoint of the bounce to a defined Vector3 (laserHit.point + reflectPos) ins$$anonymous$$d of the next gameobject collider.
We're close. Any ideas?
Never$$anonymous$$d, just had to multiply it by my range and it went on until it reached that point. Ended up with:
if (Physics.Raycast (laserRay, out laserHit, range)) {
Debug.Log(laserHit.collider.tag);
if(laserHit.collider.tag == "$$anonymous$$irror"){
hitCount+=1;
laserLine.SetVertexCount(hitCount+2);
//$$anonymous$$irror Hit : hit 1
laserLine.SetPosition (hitCount, laserHit.point);
//Reflect
Vector3 reflectPos = Vector3.Reflect(laserRay.direction, laserHit.normal);
//Hit nothing from mirror
hitCount+=1;
laserLine.SetPosition(hitCount, laserHit.point + reflectPos * range);
}else if(laserHit.collider.tag != "$$anonymous$$irror"){
laserLine.SetVertexCount(hitCount+2);
laserLine.SetPosition (hitCount+1, laserHit.point);
return;
}
} else {
hitCount+=1;
laserLine.SetPosition (hitCount, laserRay.origin + laserRay.direction * range);
return;
}
For some reason the initial laserLine end-position width is still zero, though :/
Have you tried to initially call LineRenderer.SetWidth(). Dunno the defaults, but maybe it starts off 0.
Your answer
Follow this Question
Related Questions
LineRenderer End Width Bug 2 Answers
Create a 2D line reflection (laser trace) 1 Answer
Basic LineRender Reflection 2 Answers
Dynamically adjusting LineRenderer vertexes to follow raycasts 0 Answers
LineRenderer (Laser Beam) is not following the ray it's going on the wrong direction when reflecting 1 Answer