| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- using UdonSharp;
- using UnityEngine;
- [UdonBehaviourSyncMode(BehaviourSyncMode.NoVariableSync)]
- [DisallowMultipleComponent]
- [AddComponentMenu("Artyom Scripting System/Laser Beam Controller")]
- public class LaserBeam : UdonSharpBehaviour
- {
- public LineRenderer lineRenderer; // Line Renderer for the laser beam
- public Transform laserOrigin; // Origin point of the laser
- public float maxDistance = 100.0f; // Maximum distance the laser can reach
- public LayerMask layerMask; // Layer mask to specify which colliders to detect
- public GameObject endPointObject; // GameObject to place at the end of the laser line
- [Tooltip("Whether the beam stops at trigger colliders, such as a Laser Receiver's trigger.")]
- public bool hitTriggers = true;
- void Start()
- {
- // World-space points draw correctly whichever object the Line Renderer is on
- if (lineRenderer != null) lineRenderer.useWorldSpace = true;
- if (layerMask.value == 0)
- Debug.LogWarning("[AUMSS] Laser Beam Controller: Layer Mask is Nothing, so the beam passes through everything.");
- }
- void Update()
- {
- if (lineRenderer == null || laserOrigin == null)
- {
- return;
- }
- RaycastHit hit;
- Vector3 laserEndPoint = laserOrigin.position + (laserOrigin.forward * maxDistance);
- QueryTriggerInteraction triggers = hitTriggers ? QueryTriggerInteraction.Collide : QueryTriggerInteraction.Ignore;
- if (Physics.Raycast(laserOrigin.position, laserOrigin.forward, out hit, maxDistance, layerMask.value, triggers))
- {
- laserEndPoint = hit.point;
- }
- lineRenderer.SetPosition(0, laserOrigin.position);
- lineRenderer.SetPosition(1, laserEndPoint);
- // Place the endPointObject at the end of the laser line
- if (endPointObject != null)
- {
- endPointObject.transform.position = laserEndPoint;
- }
- }
- }
|