if I am turning the mirror object, sometimes the laser won't reflect and he would just stop at the mirror and sometimes he does reflect correctly. I have the excact same script as you. It detects the mirror as the mirror, but i think the laser is getting stuck or something. Could you help me please?
Same! The RayCast() is returning false at that point so it seems like it doesn't detect the mirror then. Haven't got a fix either so a solution would be appreciated!
Maybe try increasing the size of your LineRenderer, your LineRenderer needs to have enough positions to encode all the bounces. Once you have a size of 6, you can only see 6-1 (1 position for the start) = 5 bounces.
@@vishalsaini3878 The objects need to have a collider, check if you have the correct collider if it's a 3D project make sure it is a Box Collider, capsule collider etc for 2D use BoxCollider2D. Go to edit -> project settings -> physics check if the layers can collide. Debug.DrawRay(transform.position, forward, Color.green); can be used to debug the raycast. If your problem is with the mirror name the object "Mirror" and all its children sometimes the laser is colliding with a children's collider. If all that is correct then most likely is something on your script.
If video code is not working, you can try this
using UnityEngine;
public class Laser : MonoBehaviour
{
private int _maxBounce = 20;
private int _count ;
private LineRenderer _laser;
[SerializeField]
private Vector3 _offSet;
private void Start()
{
_laser = GetComponent();
}
private void Update()
{
_count = 0;
castLaser(transform.position + _offSet, transform.up);
}
private void castLaser(Vector3 position , Vector3 direction)
{
_laser.SetPosition(0, transform.position + _offSet );
for (int i=0; i< _maxBounce; i++ )
{
Ray ray = new Ray(position, direction);
RaycastHit hit;
if(_count < _maxBounce - 1)
_count++;
if(Physics.Raycast(ray , out hit , 300 ))
{
position = hit.point;
direction = Vector3.Reflect(direction, hit.normal);
_laser.SetPosition(_count, hit.point);
if (hit.transform.tag != "Mirror")
{
for (int j = (i + 1); j < _maxBounce; j++)
{
_laser.SetPosition(j, hit.point);
}
break;
}
else
{
_laser.SetPosition(_count, hit.point);
}
}
else
{
_laser.SetPosition( _count, ray.GetPoint(300));
}
}
}
}
Awesome! keep doing such great tutorials!
thanks
Cool tutorial :) I have a problem though. The laser just passes through my mirror object. I tried having it tagged as well but it still doesn't work.
if I am turning the mirror object, sometimes the laser won't reflect and he would just stop at the mirror and sometimes he does reflect correctly. I have the excact same script as you. It detects the mirror as the mirror, but i think the laser is getting stuck or something. Could you help me please?
Same! The RayCast() is returning false at that point so it seems like it doesn't detect the mirror then. Haven't got a fix either so a solution would be appreciated!
In my project raycast is not hiting anything. How can I fix that? Mirror has a box collider and also a rigidbody. I made the script as in the video.
I answered you back on the discord server
Doesn't work!!!
Keep getting errors thet Set Position index is out of bounds
Maybe try increasing the size of your LineRenderer, your LineRenderer needs to have enough positions to encode all the bounces. Once you have a size of 6, you can only see 6-1 (1 position for the start) = 5 bounces.
not working
I cannot help you if you give me no information. All I can say is this video is still up to date and it works and other people got it working.
@@Dogma427. Laser is passing through every object and going down in Y direction on a fixed point.
@@vishalsaini3878 The objects need to have a collider, check if you have the correct collider if it's a 3D project make sure it is a Box Collider, capsule collider etc for 2D use BoxCollider2D. Go to edit -> project settings -> physics check if the layers can collide. Debug.DrawRay(transform.position, forward, Color.green); can be used to debug the raycast. If your problem is with the mirror name the object "Mirror" and all its children sometimes the laser is colliding with a children's collider.
If all that is correct then most likely is something on your script.