I just can’t figure out how this works, I’ve been figuring out how to make, what I want but It just won’t work. Lots of time, less of progress…
So here is my question.
I’m trying to make a reflecting laser, this is my result so far.
From topview it looks kinda allright, but from the side view you can see that at point 1 and 2 the “Laser” is going towards the ground and I have no idea why. And It’s not supposed to happen, I just want the laser to change from direction and not from height.
My Code:
void Update () {
Laser ();
}
void Laser(){
int maxVertCount = 5;
lineRenderer.SetVertexCount (maxVertCount);
RaycastHit hit;
Vector3 moveDir = Vector3.right;
lineRenderer.SetPosition (0, transform.localPosition);
if(Physics.Raycast(transform.position,moveDir,out hit,Mathf.Infinity, layerMask)){
lineRenderer.SetPosition(1,hit.point);
lineRenderer.SetPosition (2, Vector3.Reflect((hit.point - transform.localPosition).normalized, hit.normal));
if(Physics.Raycast(hit.point, Vector3.Reflect((hit.point - transform.localPosition).normalized, hit.normal), out hit, Mathf.Infinity, layerMask)){
lineRenderer.SetPosition(3,hit.point);
lineRenderer.SetPosition (4, Vector3.Reflect((hit.point - transform.position), hit.normal));
}
}
}