Exemple #1
0
    private void FindTargetObject(bool horizontal, out Cristal outCristal)
    {
        Vector2      outputDirection = GetDirection(horizontal);
        RaycastHit2D hit;

        Vector2 raycastOrigin = this.firepoint;

        do
        {
            hit            = Physics2D.Raycast(raycastOrigin, outputDirection);
            raycastOrigin += outputDirection * 0.002f;
        } while(hit.transform == this.transform); // make sure the hit object is not us

        if (horizontal)
        {
            horizontalEndpoint = hit.point;
        }
        else
        {
            verticalEndpoint = hit.point;
        }

        outCristal = null;
        if (hit.collider != null)
        {
            // try to get a cristal component from the object that was hit
            // outCristal will be null if the hit object does not have a cristal component
            hit.transform.TryGetComponent <Cristal>(out outCristal);
        }

        if (horizontal)
        {
            horizontalLaserTrigger.CalculateTransform(raycastOrigin, hit.point);
        }
        else
        {
            verticalLaserTrigger.CalculateTransform(raycastOrigin, hit.point);
        }
    }
Exemple #2
0
    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());
    }