Basic LineRender Reflection

Hello! I’m just starting out and trying to get a line using the Line Renderer to reflect off of an object. I’ve looked all over and there’s some added confusion as SetVertexCount has been deprecated.

Does anyone know how you would take the Vector3.Reflect example and get it working with a Line Renderer?


public class ExampleClass : MonoBehaviour {
    public Transform originalObject;
    public Transform reflectedObject;
    void Update() {
        reflectedObject.position = Vector3.Reflect(originalObject.position, Vector3.right);
    }
}

It might not be worth looking at, but here is my attempt:

public class RaycastTest : MonoBehaviour {

public float maxRayDistance = 1.0e30f;
private LineRenderer line;

void Start () {
	line = GetComponent<LineRenderer> ();
}

void FixedUpdate () {
	Laser ();

	//Debug line for raycast reference
	Debug.DrawLine (transform.position, transform.position + Vector3.up * maxRayDistance, Color.red);

}
void Laser () {
	Ray ray = new Ray (transform.position, Vector3.up);
	RaycastHit hit;
	line.SetPosition (0, ray.origin);

	if (Physics.Raycast (ray, out hit, 100)) {
		line.SetPosition (1, hit.point);
	} else {
		line.SetPosition (1, ray.GetPoint (100));
	}

	if (hit.collider.tag == "Mirrors") {
		line.SetPosition (2, hit.point + 100.0f * Vector3.Reflect (transform.forward, hit.normal));
	}
}
}

Also if anyone has an idea of where to look for more resources on this type of thing it would be greatly appreciated!
Thanks!

EDIT: So I realized my code actually sort of works once you go into the Line Renderer properties and make the Positions Size “3”

Now the issue seems to be the reflect direction is pretty far off

video of bad reflection

Is the ExampleClass position the location of the mirror?
Since the originalObject is a variable, I’ll assume so.
I’ll also assume the object has a Collider component on it, that defines the angle of the mirror (at the hit point).

The ray will start at the originalObject, and be directed towards the center of the mirror (uncompiled/tested, example only):

line.SetPosition (0, originalObject.position);
Vector3 OriginalToMirrorVector= transform.position-originalObject.position;
Ray ray = new Ray(originalObject.position, OriginalToMirrorVector);
//100 is just too arbitrary a length, so lets use the actual distance (vector magnitude) between the object and the mirror
if(Physics.Raycast (ray, out hit, OriginalToMirrorVector.magnitude))  
{
   line.SetPosition (1,  hit.point);
   if (hit.collider.tag == "Mirrors") 
   {
         //this assumes, for simplicity, that this object is the only one with the "Mirrors" tag
         reflectedObject.position=hit.point + OriginalToMirrorVector.magnitude * Vector3.Reflect (OriginalToMirrorVector, hit.normal); 
         line.SetPosition (2, reflectedObject.position);
   }
}
else //not hit!  sanity check- if this fails, you may not have the collider component enabled.
{
   line.SetPosition (1, transform.position);
}

So the weird reflection was actually due to the 100.0f I multiplied to the Vector3.Reflect (not sure why I did that!) – but remove it and everything works great.