- Home /
how can i reflect laser(Line Renderer) with the help of multiple mirrors in 3D?
hey guys,i'm building a game in 3D in which i had a source of laser, multiple mirrors and a cube and lets say that the cube is my target.i'm kinda new to unity and i have checked all other answers related to reflection stuff but they didn't work for me. this is my project and following are my problems in which i want help,
1) i want that i press space bar and laser starts.
2) i want to reflect laser when it hit the blue area on the mirror but when the grey area comes laser should terminate.
3) same goes for other mirrors but i want multiple reflections and when laser hit the cube it should terminate.
here is my code for laser. Kindly please help me i'll be very thankful.
Please post code as text and not a fraction of it as screenshot.
using UnityEngine;
using System.Collections;
public class Laser : $$anonymous$$onoBehaviour
{
private LineRenderer lr;
// Use this for initialization
void Start ()
{
lr = GetComponent<LineRenderer> ();
}
// Update is called once per frame
void Update ()
{
Fire ();
}
void Fire()
{
RaycastHit hit;
if (Physics.Raycast (transform.position, transform.forward, out hit))
{
if (hit.collider)
{
lr.SetPosition (1, new Vector3 (0, 0, hit.distance));
}
if (hit.collider.tag == "Cube")
{
Vector3 reflectLaser = Vector3.Reflect (transform.position, hit.normal);
}
}
else
{
lr.SetPosition(1, new Vector3(0, 0, 5000));
}
}
}
the tag "cube" is the blue part of the mirror.i'm new to this so can you write code for me? i've tried everything but didn't work.Please help
I'm not going to write the whole code for you, sorry.
In more or less pseudo code it could look like this:
bool ray_ter$$anonymous$$ated = false;
Vector3 ray_position = transform.position;
Vector3 ray_direction = transform.forwad;
while ( !ray_ter$$anonymous$$ated )
{
cast ray using ray_position and ray_direction
if( hit anything )
set next linerenderer point to hit position
else
set next linerenderer point to some point in the far distance in the direction of the ray
if( hit mirror )
set ray_position to hit position
set ray direction to reflected vector
else
ray_ter$$anonymous$$ated = true
}
Answer by evrolittlegames · Sep 15, 2020 at 06:39 AM
Check this tutorial https://youtu.be/pgDsOeo31Q0