Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof(LineRenderer))]
- public class LineLine : MonoBehaviour {
- public int bounces; // number of bounces
- public int maxLength; // length of line
- public bool useCheapLine; // use less points on corners or add more to make it look nice
- private LineRenderer line; // line renderer
- void Awake()
- {
- line = GetComponent<LineRenderer>();
- }
- void Update()
- {
- DrawLine();
- }
- private void DrawLine()
- {
- int vertCount = 1;
- int bounceCount = 0;
- Vector2 position = transform.position;
- Vector2 direction = transform.right;
- Vector2 lastPosition = position;
- line.SetVertexCount(vertCount);
- line.SetPosition(vertCount - 1, position);
- bool loop = true;
- while (loop == true)
- {
- if (bounceCount >= bounces)
- {
- loop = false;
- }
- RaycastHit2D hit = Physics2D.Raycast(position, direction, maxLength);
- if (hit == true)
- {
- // Get hit position and normal
- var hitPosition = hit.point;
- var hitNormal = hit.normal;
- position = hitPosition;
- /* Create Particles
- if (useParticles == true)
- {
- ParticleManager.instance.ParticleBurst(1, position, hitNormal, 170f, 3f, 0.05f, 0.20f, laserColour);
- if (Random.value < 0.01f)
- {
- ParticleManager.instance.CreateDecal(position, Random.Range(0f, 360f), 0);
- }
- }
- */
- var wall = hit.collider.GetComponent<Wall>();
- if (wall != null)
- {
- if (wall.reflect == false)
- {
- vertCount += 3;
- line.SetVertexCount(vertCount);
- line.SetPosition(vertCount - 3, Vector2.MoveTowards(position, lastPosition, 0.01f));
- line.SetPosition(vertCount - 2, position);
- line.SetPosition(vertCount - 1, position);
- break;
- }
- }
- // Calculate reflection vector
- var reflection = Vector2.Reflect(direction, hitNormal);
- // Set next direction as the reflection direction
- direction = reflection;
- // Use default weird lines or add more points for a line of constant thickness
- if (useCheapLine == true)
- {
- // Increment bounce and vert count, add new positions to renderer
- bounceCount++;
- vertCount++;
- line.SetVertexCount(vertCount);
- line.SetPosition(vertCount - 1, position);
- }
- else
- {
- // Increment bounce and vert count, add new positions to renderer
- bounceCount++;
- vertCount += 3;
- line.SetVertexCount(vertCount);
- line.SetPosition(vertCount - 3, Vector2.MoveTowards(position, lastPosition, 0.01f));
- line.SetPosition(vertCount - 2, position);
- line.SetPosition(vertCount - 1, position);
- }
- // Set last position for next line segment
- lastPosition = position;
- // Offset position to avoid raycasting into the same collider
- position = position + direction * 0.01f;
- }
- else
- {
- // No hit
- // Increment vert count, add new position maxLength in direction
- vertCount++;
- line.SetVertexCount(vertCount);
- line.SetPosition(vertCount - 1, position + (direction * maxLength));
- loop = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement