Unity 3D - Laser Bounce Reflection Tutorial

Поділитися
Вставка
  • Опубліковано 20 сер 2024

КОМЕНТАРІ • 15

  • @vishalsaini3878
    @vishalsaini3878 3 роки тому +6

    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));
    }
    }
    }
    }

  • @NegaMogekoo
    @NegaMogekoo 2 роки тому +1

    Awesome! keep doing such great tutorials!

  • @Hyphen3372
    @Hyphen3372 3 роки тому

    thanks

  • @martonsarosi9364
    @martonsarosi9364 Рік тому

    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.

  • @finnishedgames236
    @finnishedgames236 2 роки тому +1

    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?

    • @futfan9092
      @futfan9092 2 роки тому

      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!

  • @alexandres.vasconcelos8017
    @alexandres.vasconcelos8017 3 роки тому

    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.

    • @Dogma427.
      @Dogma427.  3 роки тому

      I answered you back on the discord server

  • @raduhutupasu8734
    @raduhutupasu8734 9 місяців тому

    Doesn't work!!!

  • @joachimsoetinck5651
    @joachimsoetinck5651 2 роки тому

    Keep getting errors thet Set Position index is out of bounds

    • @futfan9092
      @futfan9092 2 роки тому

      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
    @vishalsaini3878 3 роки тому

    not working

    • @Dogma427.
      @Dogma427.  3 роки тому +1

      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.

    • @vishalsaini3878
      @vishalsaini3878 3 роки тому

      @@Dogma427. Laser is passing through every object and going down in Y direction on a fixed point.

    • @Dogma427.
      @Dogma427.  3 роки тому

      @@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.