Exemple #1
0
    private void Update()
    {
        if (!PlayerMovement.canMove)
        {
            return;
        }

        float       distance           = 100f;
        ItemHandler nearestItemHandler = null;

        foreach (ItemHandler itemH in currentItemHandlers)
        {
            float newDistance = Vector3.Distance(itemH.transform.position, transform.position);
            if (distance > newDistance)
            {
                distance           = newDistance;
                nearestItemHandler = itemH;
            }
        }

        if (nearestItemHandler != null)
        {
            currentItemHandler = nearestItemHandler;

            itemName.text = nearestItemHandler.Handler.NameItem;
        }
        else
        {
            itemName.text = "";

            currentItemHandler = null;
        }


        if (currentItemHandler != null)
        {
            if (Input.GetKeyDown(KeyCode.Q))
            {
                playerItem.Use(currentItemHandler);
            }
            else if (Input.GetKeyDown(KeyCode.F))
            {
                playerItem.Take(currentItemHandler);
            }
            else if (Input.GetKeyDown(KeyCode.E))
            {
                playerItem.Eat(null);
            }
        }
        else
        {
            if (Input.GetKeyDown(KeyCode.F))
            {
                playerItem.ThrowItem();
            }
            else if (Input.GetKeyDown(KeyCode.E))
            {
                playerItem.Eat(null);
            }
            else if (Input.GetKeyDown(KeyCode.Q))
            {
                playerItem.Use(null);
            }
        }
    }