Hey everyone thanks for the help,
What I am trying to do is fairly straight forward, I am trying to display a Line Renderer that follows my bouncing/reflecting Raycast.
Here is what I have so far.
private LineRenderer lr;
public int maxReflectionCount = 3;
public float maxStepDistance = 200f;
void Start()
{
lr = GetComponent<LineRenderer>();
}
void Update()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
if (hit.collider)
{
lr.SetPosition(1, new Vector3(0, 0, hit.distance));
Reflect(this.transform.position + this.transform.forward * 0.75f, this.transform.forward, maxReflectionCount);
}
}
else
{
lr.SetPosition(1, new Vector3(0, 0, 2000));
}
}
private void Reflect(Vector3 position, Vector3 direction, int reflectionsRemaining)
{
if (reflectionsRemaining == 0)
{
return;
}
Vector3 startingPosition = position;
Ray ray = new Ray(position, direction);
RaycastHit hit2;
if (Physics.Raycast(ray, out hit2, maxStepDistance))
{
direction = Vector3.Reflect(direction, hit2.normal);
position = hit2.point;
}
else
{
position += direction * maxStepDistance;
}
Debug.DrawRay(startingPosition, position, Color.green);
Reflect(position, direction, reflectionsRemaining - 1);
}
It seems like Vector3.Reflect is the key but I can't quite figure out how to use it properly.
I have gotten it to sort of work with Gizmos
but I cant use Gizmos in the real thing and I am having trouble getting it to work with the LineRenderer. I have even tried to use the Debug.DrawRay but to no avail.
I really just want to use it to show what angles things will bounce around at. Any ideas on how I might get this working?
I appreciate any help!
EDIT 1: So I have removed all the Line Renderer stuff because I can worry about that later and I been working on what Hacky suggested. I still cant get it working properly, but here is what it currently looks like.
//private LineRenderer lr;
public int maxReflectionCount = 3;
public float maxStepDistance = 200f;
Vector3 reflDirection;
Vector3 hitPosition;
void Start()
{
//lr = GetComponent<LineRenderer>();
}
void Update()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
if (hit.collider)
{
//lr.SetPosition(1, new Vector3(0, 0, hit.distance));
Reflect(this.transform.position + this.transform.forward * 0.75f, this.transform.forward, maxReflectionCount);
}
}
else
{
//lr.SetPosition(1, new Vector3(0, 0, 2000));
}
}
private void Reflect(Vector3 position, Vector3 direction, int reflectionsRemaining)
{
if (reflectionsRemaining == 0)
{
return;
}
Vector3 startingPosition = position;
Ray ray = new Ray(position, direction);
RaycastHit hit2;
if (Physics.Raycast(ray, out hit2, maxStepDistance))
{
reflDirection = Vector3.Reflect(direction, hit2.normal);
hitPosition = hit2.point;
}
else
{
position += reflDirection * maxStepDistance;
}
Debug.DrawRay(startingPosition, reflDirection, Color.green, 1);
//Debug.DrawLine(startingPosition, position, Color.blue);
Reflect(position, direction, reflectionsRemaining - 1);
}
EDIT 2 (Big Progress!): Ok so I got the Raycasting Reflection working....and its glorious.
Here is what it looks like:
public int maxReflectionCount = 5;
public float maxStepDistance = 200f;
void Start()
{
}
void Update()
{
Laser();
}
void Laser()
{
DrawReflectionPattern(this.transform.position + this.transform.forward * 0.75f, this.transform.forward, maxReflectionCount);
}
private void DrawReflectionPattern(Vector3 position, Vector3 direction, int reflectionsRemaining)
{
if (reflectionsRemaining == 0)
{
return;
}
Vector3 startingPosition = position;
Ray ray = new Ray(position, direction);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, maxStepDistance))
{
direction = Vector3.Reflect(direction, hit.normal);
position = hit.point;
}
else
{
position += direction * maxStepDistance;
}
//Gizmos.color = Color.yellow;
//Gizmos.DrawLine(startingPosition, position);
Debug.DrawLine(startingPosition, position, Color.blue);
DrawReflectionPattern(position, direction, reflectionsRemaining - 1);
}
Now I just have to figure out how to connect the Line Renderer to it and we are in business!