- Home /
Reflecting & Reflacting (tag dependant) Laser updated each frame?
I have tried a few different methods with Raycast Reflection and LineRenderers, but none seem to work correctly. This one, the only one that semi-works, only sets itself at the start and never updates for a moved object; and goes really wonky and twists and goes up and down slightly and gets distorted severely (no refraction in this yet, don't bother pointing that out). Any ideas? I've been stuck on this for a long while.
Not-working as per requirements (for an example of what I'm trying to do, entirely new code is a viable option):
@script RequireComponent(LineRenderer)
private var goTransform:Transform;
private var lineRenderer:LineRenderer;
private var ray:Ray;
private var hit:RaycastHit;
private var inDirection:Vector3;
public var nReflections:int = 2;
private var nPoints:int;
function Awake ()
{
goTransform = this.GetComponent(Transform);
lineRenderer = this.GetComponent(LineRenderer);
}
function Update () {
nReflections = Mathf.Clamp(nReflections,1,nReflections);
ray = new Ray(goTransform.position,goTransform.forward);
Debug.DrawRay (goTransform.position,goTransform.forward * 100, Color.magenta);
nPoints = nReflections;
lineRenderer.SetVertexCount(nPoints);
lineRenderer.SetPosition(0,goTransform.position);
for(var i:int=0;i<=nReflections;i++) {
if(i == 0) {
if(Physics.Raycast(ray.origin,ray.direction, hit, 100)) {
if(hit.transform.CompareTag("LaserReflector")) {
inDirection = Vector3.Reflect(ray.direction, hit.normal);
ray = new Ray(hit.point,inDirection);
Debug.DrawRay (hit.point, hit.normal*3, Color.blue);
Debug.DrawRay (hit.point, inDirection*100, Color.magenta);
Debug.Log("Object name: " + hit.transform.name);
if(nReflections==1)
{
lineRenderer.SetVertexCount(++nPoints);
}
lineRenderer.SetPosition(i+1, hit.point);
}
}
}
else
{
if(Physics.Raycast(ray.origin,ray.direction, hit, 100)) {
if(hit.transform.CompareTag("LaserReflector")) {
inDirection = Vector3.Reflect(inDirection, hit.normal);
ray = new Ray(hit.point, inDirection);
Debug.DrawRay (hit.point, hit.normal*3, Color.blue);
Debug.DrawRay (hit.point, inDirection*100, Color.magenta);
Debug.Log("Object name: " + hit.transform.name);
lineRenderer.SetVertexCount(++nPoints);
lineRenderer.SetPosition(i+1,hit.point);
}
}
}
}
}
Your answer
Follow this Question
Related Questions
GetComponent() problems 2 Answers
Raycasting fail 1 Answer
Why UI layer is being ignored by the raycast? 0 Answers
Trouble with raycast hitting a tagged object 2 Answers
Disabling a lineRender or Ray when there is no target 1 Answer