コード例 #1
0
    void Update( )
    {
        GameObject currentlyLookintAt = LookForInteractableObject( );

        if (currentlyLookintAt)
        {
            helpLabel.text = "Looking at: " + currentlyLookintAt.name;
            crosshair.CanIntaract();

            if (Input.GetMouseButtonDown(0)) // We have an IInteractable and we pressed LMB
            {
                currentlyLookintAt.GetComponent <IInteractable>()?.OnPress();
                pressing = true;
            }

            if (Input.GetMouseButtonUp(0))  // We have an IInteractable and we released LMB
            {
                currentlyLookintAt.GetComponent <IInteractable>()?.OnRelease();
                pressing = false;
            }
        }
        else
        {
            helpLabel.text = "Nothing to interact with...";
            crosshair.Normal();
        }


        if (currentlyLookintAt != lastLookedObject && lastLookedObject && pressing)          // We moved away so we treat it as release of LMB
        {
            lastLookedObject.GetComponent <IInteractable>( )?.OnRelease( );
            pressing = false;
        }

        if (currentlyLookintAt == lastLookedObject)           // Do nothing, we are still looking at the same thing
        {
            return;
        }

        if (currentlyLookintAt)
        {
            currentlyLookintAt.GetComponent <IInteractable>( )?.OnOverEnter( );
        }

        if (lastLookedObject)
        {
            lastLookedObject.GetComponent <IInteractable>( )?.OnOverExit( );
        }

        lastLookedObject = currentlyLookintAt;
    }