Exemplo n.º 1
0
    public void AutoAim()
    {
        Vector3 pos    = this.gameObject.transform.position;
        float   thresh = 0.7f;

        Collider2D[] overlaps = Physics2D.OverlapCircleAll(pos, AutoAimRange);

        float      min_dist   = Mathf.Infinity;
        GameObject old_target = target;

        target = null;
        foreach (Collider2D overlap in overlaps)
        {
            if (overlap.gameObject.CompareTag("Enemy"))
            {
                Vector2 hit_pos = new Vector2(overlap.gameObject.transform.position.x, overlap.gameObject.transform.position.y);
                Vector2 sub     = new Vector2(pos.x - hit_pos.x, pos.y - hit_pos.y);
                if (min_dist > sub.magnitude + thresh)
                {
                    target   = overlap.gameObject;
                    min_dist = sub.magnitude;
                }
            }
        }

        if (target != null)
        {
            // Vector3 target_center = target.GetComponent<Collider2D>().bounds.center;
            Vector3 target_center = target.transform.position;
            Vector2 dir           = (target_center - gameObject.transform.position).normalized;
            //Vector2 dir = (target_center - m_FirePoint.transform.position).normalized;
            m_MovementComponent.FlipSprite(dir.x);
            Quaternion rot = GameplayStatics.GetRotationFromDir(dir);
            m_PlayerHand.GetComponent <SpriteRenderer>().transform.rotation = rot;

            target.GetComponentInChildren <TargetHighlightComponent>().ToggleHighlight(true);
            if (old_target)
            {
                if (old_target != target)
                {
                    var shadow = old_target.GetComponentInChildren <TargetHighlightComponent>();
                    if (shadow)
                    {
                        shadow.ToggleHighlight(false);
                    }
                }
            }
        }

        m_SpellComponent.SetTarget(target);
    }