87 lines
2.2 KiB
C#
87 lines
2.2 KiB
C#
using UnityEngine;
|
|
|
|
[ExecuteAlways]
|
|
[RequireComponent(typeof(LineRenderer))]
|
|
public class LaserBeam : MonoBehaviour
|
|
{
|
|
[Header("Laser Settings")]
|
|
public float maxDistance = 20f;
|
|
public LayerMask collisionMask = ~0;
|
|
|
|
[Header("Laser Appearance")]
|
|
public Color laserColor = Color.red;
|
|
public float laserWidth = 0.05f;
|
|
public float emissionStrength = 5f;
|
|
public float scrollSpeed = 1f;
|
|
|
|
private LineRenderer line;
|
|
|
|
void Reset()
|
|
{
|
|
SetupLaserRenderer();
|
|
}
|
|
|
|
void Awake()
|
|
{
|
|
SetupLaserRenderer();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (!line) return;
|
|
|
|
Vector3 start = transform.position;
|
|
Vector3 direction = transform.forward;
|
|
|
|
RaycastHit hit;
|
|
Vector3 end;
|
|
|
|
if (Physics.Raycast(start, direction, out hit, maxDistance, collisionMask))
|
|
{
|
|
end = hit.point;
|
|
}
|
|
else
|
|
{
|
|
end = start + direction * maxDistance;
|
|
}
|
|
|
|
line.SetPosition(0, start);
|
|
line.SetPosition(1, end);
|
|
}
|
|
|
|
[ContextMenu("Setup Laser Renderer")]
|
|
public void SetupLaserRenderer()
|
|
{
|
|
line = GetComponent<LineRenderer>();
|
|
if (line == null)
|
|
line = gameObject.AddComponent<LineRenderer>();
|
|
|
|
line.positionCount = 2;
|
|
line.useWorldSpace = true;
|
|
line.loop = false;
|
|
line.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
|
line.receiveShadows = false;
|
|
line.widthMultiplier = laserWidth;
|
|
|
|
// Try to load the custom shader
|
|
Shader laserShader = Shader.Find("Custom/EmissiveLaser");
|
|
if (laserShader != null)
|
|
{
|
|
Material laserMat = new Material(laserShader);
|
|
laserMat.SetColor("_Color", laserColor);
|
|
laserMat.SetFloat("_Emission", emissionStrength);
|
|
laserMat.SetFloat("_ScrollSpeed", scrollSpeed);
|
|
line.material = laserMat;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("Custom/EmissiveLaser shader not found. Using fallback material.");
|
|
line.material = new Material(Shader.Find("Sprites/Default"));
|
|
line.material.color = laserColor;
|
|
}
|
|
|
|
line.startColor = laserColor;
|
|
line.endColor = laserColor;
|
|
}
|
|
}
|