Advertisement
zaboodable

Line Bounce

Jan 26th, 2017
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(LineRenderer))]
  4. public class LineLine : MonoBehaviour {
  5.  
  6.     public int bounces;         // number of bounces
  7.     public int maxLength;       // length of line
  8.     public bool useCheapLine;   // use less points on corners or add more to make it look nice
  9.     private LineRenderer line;   // line renderer
  10.  
  11.     void Awake()
  12.     {
  13.         line = GetComponent<LineRenderer>();
  14.     }
  15.  
  16.     void Update()
  17.     {
  18.         DrawLine();
  19.     }
  20.  
  21.     private void DrawLine()
  22.     {
  23.         int vertCount = 1;
  24.         int bounceCount = 0;
  25.  
  26.         Vector2 position = transform.position;
  27.         Vector2 direction = transform.right;
  28.         Vector2 lastPosition = position;
  29.  
  30.         line.SetVertexCount(vertCount);
  31.         line.SetPosition(vertCount - 1, position);
  32.  
  33.         bool loop = true;
  34.         while (loop == true)
  35.         {
  36.             if (bounceCount >= bounces)
  37.             {
  38.                 loop = false;
  39.             }
  40.             RaycastHit2D hit = Physics2D.Raycast(position, direction, maxLength);
  41.  
  42.             if (hit == true)
  43.             {
  44.                 // Get hit position and normal
  45.                 var hitPosition = hit.point;
  46.                 var hitNormal = hit.normal;
  47.                 position = hitPosition;
  48.  
  49.                 /* Create Particles
  50.                 if (useParticles == true)
  51.                 {
  52.                     ParticleManager.instance.ParticleBurst(1, position, hitNormal, 170f, 3f, 0.05f, 0.20f, laserColour);
  53.                     if (Random.value < 0.01f)
  54.                     {
  55.                         ParticleManager.instance.CreateDecal(position, Random.Range(0f, 360f), 0);
  56.                     }
  57.                 }
  58.                 */
  59.  
  60.                 var wall = hit.collider.GetComponent<Wall>();
  61.                 if (wall != null)
  62.                 {
  63.                     if (wall.reflect == false)
  64.                     {
  65.                         vertCount += 3;
  66.                         line.SetVertexCount(vertCount);
  67.                         line.SetPosition(vertCount - 3, Vector2.MoveTowards(position, lastPosition, 0.01f));
  68.                         line.SetPosition(vertCount - 2, position);
  69.                         line.SetPosition(vertCount - 1, position);
  70.                         break;
  71.                     }
  72.                 }
  73.  
  74.                 // Calculate reflection vector
  75.                 var reflection = Vector2.Reflect(direction, hitNormal);
  76.  
  77.                 // Set next direction as the reflection direction
  78.                 direction = reflection;
  79.  
  80.                 // Use default weird lines or add more points for a line of constant thickness
  81.                 if (useCheapLine == true)
  82.                 {
  83.                     // Increment bounce and vert count, add new positions to renderer
  84.                     bounceCount++;
  85.                     vertCount++;
  86.                     line.SetVertexCount(vertCount);
  87.                     line.SetPosition(vertCount - 1, position);
  88.                 }
  89.                 else
  90.                 {
  91.                     // Increment bounce and vert count, add new positions to renderer
  92.                     bounceCount++;
  93.                     vertCount += 3;
  94.                     line.SetVertexCount(vertCount);
  95.                     line.SetPosition(vertCount - 3, Vector2.MoveTowards(position, lastPosition, 0.01f));
  96.                     line.SetPosition(vertCount - 2, position);
  97.                     line.SetPosition(vertCount - 1, position);
  98.                 }
  99.  
  100.                 // Set last position for next line segment
  101.                 lastPosition = position;
  102.  
  103.                 // Offset position to avoid raycasting into the same collider
  104.                 position = position + direction * 0.01f;
  105.             }
  106.             else
  107.             {
  108.                 // No hit
  109.                 // Increment vert count, add new position maxLength in direction
  110.                 vertCount++;
  111.                 line.SetVertexCount(vertCount);
  112.                 line.SetPosition(vertCount - 1, position + (direction * maxLength));
  113.                 loop = false;
  114.             }
  115.         }
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement