예제 #1
0
    void RaycastForHighlightable()
    {
        if (EventSystem.current.IsPointerOverGameObject())
        {
            HighlightOff();
            return;
        }

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        Collider[] colliders = Physics.OverlapCapsule(ray.origin, ray.origin + ray.direction * distance, radius, highlaytableLayers, QueryTriggerInteraction.Collide);

        float distanceFromRay = float.MaxValue;
        bool  highlighted     = false;

        foreach (Collider col in colliders)
        {
            HighlightableObject highlightScript = col.gameObject.GetComponentInParent <HighlightableObject>();
            if (highlightScript != null && highlightScript.canBeHighlighted)
            {
                LemmingStateController lemming = highlightScript.GetComponent <LemmingStateController>();
                if (lemming != null)
                {
                    if (lemming.Team != playerTeam)
                    {
                        continue;
                    }
                }

                float angle = Vector3.Angle(ray.direction, (col.bounds.center - Camera.main.transform.position));
                float dist  = Mathf.Sin(Mathf.Deg2Rad * angle) * Vector3.Distance(col.bounds.center, Camera.main.transform.position);
                if (dist < distanceFromRay)
                {
                    distanceFromRay   = distance;
                    highlightedObject = highlightScript;
                    highlighted       = true;
                }
            }
        }

        if (highlighted && !isHighlighting)
        {
            HighlightOn();
        }
        else if (!highlighted && isHighlighting)
        {
            HighlightOff();
        }
    }