Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(LineRenderer))]
- public class Laser : MonoBehaviour
- {
- // How many bounce counts
- [Range(1, 5)]
- [SerializeField] private int _maxBounce = 3;
- // Maximum line lenth
- [SerializeField] private float _maxDistance = 50f;
- public int _count;
- private LineRenderer lineRenderer;
- public Transform linePoint;
- private void Start()
- {
- lineRenderer = GetComponent<LineRenderer>();
- }
- private void Update()
- {
- _count = 0;
- lineRenderer.SetVertexCount(2);
- lineRenderer.SetPosition(0, transform.position);
- RayCast(transform.position, transform.right);
- }
- private bool RayCast(Vector2 pos, Vector2 dir)
- {
- RaycastHit2D hit2D = Physics2D.Raycast(pos, dir, _maxDistance);
- if(hit2D && _count <= _maxBounce - 1)
- {
- _count++;
- var reflectAngle = Vector2.Reflect(dir, hit2D.normal);
- lineRenderer.SetVertexCount(_count + 1);
- lineRenderer.SetPosition(_count, hit2D.point);
- RayCast(hit2D.point + reflectAngle, reflectAngle);
- return true;
- }
- else if(hit2D == false)
- {
- lineRenderer.SetVertexCount(_count + 2);
- lineRenderer.SetPosition(_count + 1, pos + dir * _maxDistance);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement