Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Laser : MonoBehaviour
- {
- public int reflections;
- public float maxLength;
- private LineRenderer lineRenderer;
- private Ray ray;
- private RaycastHit hit;
- private void Awake()
- {
- lineRenderer = GetComponent<LineRenderer>();
- }
- private void Update()
- {
- ray = new Ray(transform.position, transform.right);
- lineRenderer.positionCount = 1;
- lineRenderer.SetPosition(0, transform.position);
- float remainingLength = maxLength;
- for (int i = 0; i < reflections; i++)
- {
- if (Physics.Raycast(ray.origin, ray.direction, out hit, remainingLength))
- {
- lineRenderer.positionCount += 1;
- lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
- remainingLength -= Vector3.Distance(ray.origin, hit.point);
- ray = new Ray(hit.point, Vector3.Reflect(ray.direction, hit.normal));
- if (hit.collider.tag != "Ground")
- break;
- }
- else
- {
- lineRenderer.positionCount += 1;
- lineRenderer.SetPosition(lineRenderer.positionCount - 1, ray.origin + ray.direction * remainingLength);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement