/// <param name="laserDirection">Normalized direction of the laser</param> public void GetLaserPoints(Vector2 laserDirection, ref List <Vector3> points, Laser laser) { laser.AddSubscriber(OnLaserStateChange); points.Add(this.firepoint); Vector2 horizontalDir = this.GetDirection(true); Vector2 verticalDir = this.GetDirection(false); Vector2 redirectedDirection = Vector2.zero; Cristal nextCristal = null; Vector2 nextPoint = Vector2.zero; // if horizontal direction and laser direction are exactly opposite if (Vector2.Dot(horizontalDir, laserDirection) == -1.0f) { nextCristal = nextVerticalCristal; redirectedDirection = verticalDir; nextPoint = verticalEndpoint; } else if (Vector2.Dot(verticalDir, laserDirection) == -1.0f) { nextCristal = nextHorizontalCristal; redirectedDirection = horizontalDir; nextPoint = horizontalEndpoint; } if (nextCristal) { nextCristal.GetLaserPoints(redirectedDirection, ref points, laser); } // if sqrd mag > 0, it means it's not vector 0 anymore // which means at least one of the conditions above passed else if (nextPoint.sqrMagnitude > 0.0f) { points.Add(nextPoint); } }
public void Shoot(Vector2 direction) { notifyStateChangeToSubscribers?.Invoke(false); notifyStateChangeToSubscribers = null; List <Vector3> points = new List <Vector3>(); points.Add(firepoint.position); RaycastHit2D hit = Physics2D.Raycast(firepoint.position, direction); if (hit.collider) { Cristal cristal = null; if (hit.collider.gameObject.tag == "hittable") { hit.collider.gameObject.SendMessage("HitByRay", direction); } else if (hit.transform.TryGetComponent <Cristal>(out cristal)) { cristal.GetLaserPoints(direction, ref points, this); } else { points.Add(hit.point); } } else { points.Add(hit.point); } laserTrigger.CalculateTransform(firepoint.position, hit.point); lineRenderer.positionCount = points.Count; lineRenderer.SetPositions(points.ToArray()); }