Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class LaserEmitter : MonoBehaviour
- {
- private LineRenderer lr;
- public float laserSize = 0.1f;
- [Header("Transforms")]
- public Transform LaserHit;
- public Transform emitPoint;
- public Transform endPoint;
- public GameObject sparks;
- void Start()
- {
- lr = GetComponent<LineRenderer>();
- }
- // Update is called once per frame
- void Update()
- {
- lr.startWidth = laserSize;
- lr.endWidth = laserSize; // Allows me to easily and precisely change width of laser in inspector without dragging points
- lr.SetPosition(0, emitPoint.position); // Sets beginning of laser to empty gameObject EmitPoint
- RaycastHit hit;
- // public static int RaycastNonAlloc(Vector3 origin, Vector3 direction, RaycastHit[] results, [Internal.DefaultValue("Mathf.Infinity")] float maxDistance, [Internal.DefaultValue("DefaultRaycastLayers")] int layerMask, QueryTriggerInteraction queryTriggerInteraction)
- if (Physics.Raycast(emitPoint.position, transform.right, out hit, Mathf.Infinity, -1, QueryTriggerInteraction.Ignore)) // Laser does not ignore regular colliders, but ignores triggers
- {
- if (hit.collider)
- {
- lr.SetPosition(1, hit.point); // Sets end of laser to point of collision of raycast
- endPoint.position = hit.point; // Places endPoint gameObject at end of laser
- if (hit.collider.tag == "Reflector" || hit.collider.tag == "Player") // Check if collider hit is reflective OR player
- {
- sparks.SetActive(false); // Disables sparks on reflective objects
- }
- else
- {
- sparks.SetActive(true); // Otherwise, re-enable sparks
- }
- }
- }
- else
- {
- lr.SetPosition(1, emitPoint.position + transform.right * 100); // If there is no collider in raycast, extend line for 100 units
- // emitPoint.position is added transform.right to make x and y-coordinate of both points of LineRenderer be the same
- endPoint.position = lr.GetPosition(1);
- sparks.SetActive(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment