Advertisement
Guest User

Untitled

a guest
Jun 9th, 2020
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1.     using System.Collections;
  2.     using System.Collections.Generic;
  3.     using UnityEngine;
  4.  
  5. public class Laser : MonoBehaviour
  6. {
  7.     public int reflections;
  8.     public float maxLength;
  9.  
  10.     private LineRenderer lineRenderer;
  11.     private Ray ray;
  12.     private RaycastHit hit;
  13.  
  14.  
  15.     private void Awake()
  16.     {
  17.         lineRenderer = GetComponent<LineRenderer>();
  18.     }
  19.  
  20.     private void Update()
  21.     {
  22.         ray = new Ray(transform.position, transform.right);
  23.         lineRenderer.positionCount = 1;
  24.         lineRenderer.SetPosition(0, transform.position);
  25.  
  26.         float remainingLength = maxLength;
  27.  
  28.         for (int i = 0; i < reflections; i++)
  29.         {
  30.             if (Physics.Raycast(ray.origin, ray.direction, out hit, remainingLength))
  31.             {
  32.                 lineRenderer.positionCount += 1;
  33.                 lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
  34.                 remainingLength -= Vector3.Distance(ray.origin, hit.point);
  35.                 ray = new Ray(hit.point, Vector3.Reflect(ray.direction, hit.normal));
  36.                 if (hit.collider.tag != "Ground")
  37.                     break;
  38.             }
  39.             else
  40.             {
  41.                 lineRenderer.positionCount += 1;
  42.                 lineRenderer.SetPosition(lineRenderer.positionCount - 1, ray.origin + ray.direction * remainingLength);
  43.             }
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement