- Home /
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
Answer by wyatts · Feb 06, 2017 at 09:16 PM
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.
I'm trying to make the Line Renderer reflect multiple times on any number of objects that appear in the path of the rendered line. I'm trying to do it by increasing the number of vertices. But it limits the number of times I can replicate the behaviour and increases the length of the script. Is there a way to do it using Loops and Arrays?
I know that the SetVertexCount thing has been deprecated. Someone else posted something like this:
Vector3[] points = new Vector3[3];
//Something something ...
lineRenderer.positionCount = points.Length;
lineRenderer.SetPositions(points);
I was actually looking for a way to dynamically reflect the Raycast so that when if the rotation of the reflective object changes, the Line Renderer adapts to the change. So I looked a lot before I found this post. He explains almost each line and the code works.
Link : http://www.41post.com/4162/program$$anonymous$$g/unity-raycast-reflection
Looks pretty complicated and he uses stuff that is very out of date. Unity does support reflections through Vector3.reflect
Answer by Glurth · Feb 02, 2017 at 10:56 PM
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);
}
Thanks for your input! I tried out your method and couldn't get it working. Not sure where it breaks down.