Exemple #1
0
    public void GetInteractions()
    {
        //find all possible interactable objects in range
        Collider2D[] possibleInteractions;
        possibleInteractions = Physics2D.OverlapCircleAll(myTransform.position, interactionDistance, LayerMaskHandler.instance.interactableLayer);
        if (possibleInteractions.Length == 0)
        {
            myInteractionObject = null; return;
        }

        //find closest interactable object in range
        float distance          = 1000f;//start at an impossiblity
        int   interactionObject = 0;

        for (int i = 0; i < possibleInteractions.Length; i++)
        {
            float newDistance = (possibleInteractions[i].transform.position - myTransform.position).magnitude;
            if (newDistance < distance)
            {
                distance = newDistance; interactionObject = i;
            }
        }
        myInteractionObject = possibleInteractions[interactionObject].gameObject;
        InteractionBase myInteraction = myInteractionObject.GetComponent("InteractionBase") as InteractionBase;

        myInteraction.Use();
    }