Facebook
From Cute Goat, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 82
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(LineRenderer))]
  6. public class _LaserSystem : MonoBehaviour
  7. {
  8.     [SerializeField] private int _Reflections;
  9.     [SerializeField] private float _MaxLength;
  10.  
  11.     private LineRenderer _lineRenderer;
  12.     private Ray _ray;
  13.     private RaycastHit _hit;
  14.     private Vector3 _dir;
  15.     // Start is called before the first frame update
  16.     private void Awake()
  17.     {
  18.         _lineRenderer = GetComponent<LineRenderer>();
  19.     }
  20.  
  21.     // Update is called once per frame
  22.     void Update()
  23.     {
  24.         Laser();
  25.     }
  26.  
  27.  
  28.     void Laser()
  29.     {
  30.         _ray = new Ray(transform.position, transform.forward);
  31.         _lineRenderer.positionCount = 1;
  32.         _lineRenderer.SetPosition(0, transform.position); ;
  33.  
  34.         float reaminingLength = _MaxLength;
  35.         for (int i = 0; i < _Reflections; i++)
  36.         {
  37.             if (Physics.Raycast(_ray.origin, _ray.direction, out _hit, reaminingLength))
  38.             {
  39.                 _lineRenderer.positionCount += 1;
  40.                 _lineRenderer.SetPosition(_lineRenderer.positionCount - 1, _hit.point);
  41.                 reaminingLength -= Vector3.Distance(_ray.origin, _hit.point);
  42.                 _ray = new Ray(_hit.point, Vector3.Reflect(_ray.direction, _hit.normal));
  43.                 if (_hit.collider.tag != "Mirror")
  44.                     break;
  45.             }
  46.             else
  47.             {
  48.                 _lineRenderer.positionCount += 1;
  49.                 _lineRenderer.SetPosition(_lineRenderer.positionCount - 1,_ray.origin + _ray.direction * reaminingLength);
  50.             }
  51.         }
  52.     }
  53. }
  54.