Advertisement
Guest User

Untitled

a guest
Jun 9th, 2020
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.44 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 Ray2D ray;
  12.         private RaycastHit2D hit;
  13.  
  14.  
  15.         private void Awake()
  16.         {
  17.             lineRenderer = GetComponent<LineRenderer>();
  18.         }
  19.  
  20.         private void Update()
  21.         {
  22.             ray = new Ray2D(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.                 hit = Physics2D.Raycast(ray.origin, ray.direction);
  31.  
  32.                     lineRenderer.positionCount += 1;
  33.                     lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
  34.                     ///remainingLength -= Vector2.Distance(ray.origin, hit.point);
  35.                     ray = new Ray2D(hit.point, Vector2.Reflect(ray.direction, hit.normal));
  36.  
  37.                 //else
  38.                 {
  39.                     //lineRenderer.positionCount += 1;
  40.                     //lineRenderer.SetPosition(lineRenderer.positionCount - 1, ray.origin + ray.direction * remainingLength);
  41.                 }
  42.            
  43.             }  
  44.         }
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement