- Home /
Question by
wewewu · Jun 01, 2020 at 10:40 AM ·
c#raycastreflectionline rendererlaser
Shoot Laser
I would like to make a laser weapon and reflect on objects depending on the max reflection time with line renderer and raycast and I have got this script which is not working:
else if (Input.GetKey(shootkey) && weapon == Weapon.Laser)
{
int reflectedTimes = 0;
RaycastHit hit;
Ray ray = new Ray();
Vector3 origin = shootPosition.position;
Vector3 direction = cam.forward;
for (bool cast = true; cast == true; )
{
ray.origin = origin;
ray.direction = direction;
lr.SetPosition(2 * reflectedTimes, origin);
if (Physics.Raycast(ray, out hit, 100f))
{
if (hit.collider)
{
if (!lr.gameObject.activeSelf)
{
lr.gameObject.SetActive(true);
}
lr.SetPosition(2 * reflectedTimes + 1, hit.point);
origin = hit.point;
direction = Vector3.Reflect(ray.direction, hit.normal);
reflectedTimes += 1;
}
}
else
{
if (!lr.gameObject.activeSelf)
{
lr.gameObject.SetActive(true);
}
lr.SetPosition(2 * reflectedTimes + 1, direction * 100f);
cast = false;
}
if (reflectedTimes > reflect)
{
cast = false;
}
}
}
else
{
lr.gameObject.SetActive(false);
}
Any help will be appreciated.
Comment
Your answer
Follow this Question
Related Questions
Updating line positions with reflections 1 Answer
Can't get a laser working properly. 2 Answers
Why are the reflected bullets in my 2D game acting strange? 1 Answer
Multiple Cars not working 1 Answer
LineRenderer (Laser Beam) is not following the ray it's going on the wrong direction when reflecting 1 Answer