using System.Collections; using System.Collections.Generic; using UnityEngine; public class Laser : MonoBehaviour { public bool flipDirection; public int power = 1; float maxDistance = 1000.0f; bool isHorizontal; LineRenderer laserLine; GameObject lastChargePoint; void Start() { laserLine = GetComponent(); isHorizontal = GetComponent().isHorizontal; } void Update() { laserLine.SetPosition(0, transform.position); RaycastHit2D hit; if (isHorizontal) { float endpointY = flipDirection ? transform.position.y - maxDistance : transform.position.y + maxDistance; hit = Physics2D.Raycast(transform.position, flipDirection ? Vector2.down : Vector2.up); if (hit.collider) endpointY = hit.collider.transform.position.y; laserLine.SetPosition(1, new Vector3(transform.position.x, endpointY, 0)); } else { float endpointX = flipDirection ? transform.position.x - maxDistance : transform.position.x + maxDistance; hit = Physics2D.Raycast(transform.position, flipDirection ? Vector2.left : Vector2.right); if (hit.collider) endpointX = hit.collider.transform.position.x; laserLine.SetPosition(1, new Vector3(endpointX, transform.position.y, 0)); } if (hit.collider && hit.collider.tag == "Charge Point") { if (hit.collider.gameObject != lastChargePoint) hit.collider.GetComponent().adjustCharges(power); lastChargePoint = hit.collider.gameObject; } else { if (lastChargePoint) lastChargePoint.GetComponent().adjustCharges(power * -1); lastChargePoint = null; } } void OnDrawGizmosSelected() { float distance = 1.0f; isHorizontal = GetComponent().isHorizontal; Vector3 offset = isHorizontal ? new Vector3(0, distance, 0) : new Vector3(distance, 0, 0); Gizmos.color = Color.blue; Gizmos.DrawLine(transform.position, flipDirection ? transform.position - offset : transform.position + offset); } }