106 lines
3.1 KiB
C#
106 lines
3.1 KiB
C#
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(LineRenderer))]
|
|
public class ChaseLaserBeam : MonoBehaviour
|
|
{
|
|
[Header("Laser Setup")]
|
|
public Transform laserOrigin;
|
|
public float maxDistance = 20f;
|
|
public LayerMask collisionMask;
|
|
public Color laserColor = Color.red;
|
|
public float laserWidth = 0.05f;
|
|
|
|
[Header("Visuals")]
|
|
public float scrollSpeed = 1f;
|
|
public float emissionStrength = 5f;
|
|
|
|
[Header("Performance")]
|
|
public float updateRate = 0.02f;
|
|
|
|
private LineRenderer line;
|
|
private Vector3 laserStart, laserEnd;
|
|
|
|
void Awake()
|
|
{
|
|
line = GetComponent<LineRenderer>();
|
|
SetupLaserRenderer();
|
|
}
|
|
|
|
void OnEnable()
|
|
{
|
|
line.enabled = true;
|
|
InvokeRepeating(nameof(UpdateLaser), 0f, updateRate);
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
CancelInvoke(nameof(UpdateLaser));
|
|
if (line != null)
|
|
line.enabled = false;
|
|
}
|
|
|
|
void UpdateLaser()
|
|
{
|
|
laserStart = laserOrigin ? laserOrigin.position : transform.position;
|
|
Vector3 direction = transform.forward;
|
|
|
|
if (Physics.Raycast(laserStart, direction, out RaycastHit hit, maxDistance, collisionMask))
|
|
{
|
|
laserEnd = hit.point;
|
|
|
|
if (hit.collider.CompareTag("Player"))
|
|
{
|
|
Debug.Log("⚠️ Player hit by chase laser!");
|
|
|
|
ChasePlayerController player = hit.collider.GetComponent<ChasePlayerController>();
|
|
if (player != null && !player.waitingForGameOver)
|
|
{
|
|
player.moveSpeed = 0;
|
|
player.waitingForGameOver = true;
|
|
player.StartCoroutine(player.PlayStateAndGameOver(player.fallingStateName, player.fallingShortHash));
|
|
}
|
|
// TODO: Trigger player damage or effects
|
|
}
|
|
}
|
|
else
|
|
{
|
|
laserEnd = laserStart + direction * maxDistance;
|
|
}
|
|
|
|
line.enabled = true; // Force enabled in case something disables it
|
|
line.SetPosition(0, laserStart);
|
|
line.SetPosition(1, laserEnd);
|
|
}
|
|
|
|
void SetupLaserRenderer()
|
|
{
|
|
line.useWorldSpace = true;
|
|
line.positionCount = 2;
|
|
line.loop = false;
|
|
line.widthMultiplier = laserWidth;
|
|
line.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
|
line.receiveShadows = false;
|
|
|
|
// Try using custom emissive 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
|
|
{
|
|
// Fallback shader
|
|
Material fallback = new Material(Shader.Find("Sprites/Default"));
|
|
fallback.color = laserColor;
|
|
line.material = fallback;
|
|
}
|
|
|
|
line.startColor = laserColor;
|
|
line.endColor = laserColor;
|
|
}
|
|
}
|