Пример #1
0
    public void detachKey()  //Detach this key from the current keyhole.
    {
        if (currentKeyhole != null)
        {
            Keyhole k = currentKeyhole.GetComponent <Keyhole>();
            k.currentKey = null;
            k.checkCorrectKey();
            currentKeyhole = null;
        }

        RB.isKinematic     = false;
        RB.useGravity      = false;
        RB.velocity        = Vector3.zero;
        RB.angularVelocity = Vector3.zero;
    }
Пример #2
0
    public void placeKey()             //Places this key in the closest keyhole.
    {
        if (keyholesInRange.Count > 0) //Only run if there is any keyholes in range.
        {
            //Set a standard to compare with other keyholes if any.
            GameObject closestKeyhole   = keyholesInRange[0];
            float      shortestDistance = Vector3.Distance(transform.position, closestKeyhole.transform.position);

            for (int i = 1; i < keyholesInRange.Count; i++)                                                         //Running through the list.
            {
                if (shortestDistance > Vector3.Distance(transform.position, keyholesInRange[i].transform.position)) //If the distance is less then the previus distance then the new closest keyhole is the current keyhole.
                {
                    closestKeyhole = keyholesInRange[i];                                                            //New closest keyhole from index.
                }
            }

            if (closestKeyhole.GetComponent <Keyhole>() != null)     //If the currenct closest keyhole has the script keyhole.
            {
                Keyhole k = closestKeyhole.GetComponent <Keyhole>(); //Getting the keyhole script.

                if (k.currentKey == null)                            //If there already is a key in the current keyhole.
                {
                    currentKeyhole = closestKeyhole;                 //The current keyhole is now where this key is placed.
                    k.currentKey   = this.gameObject;                //This key is now the current key for the current keyhole.
                    RB.isKinematic = true;                           //The key is now still.
                    k.checkCorrectKey();                             //Tell the current keyhole to check if this key is the correct key.

                    //Removing this key from activating the highlight for all keyholes in range.
                    for (int i = 0; i < keyholesInRange.Count; i++)
                    {
                        if (keyholesInRange[i].GetComponent <Keyhole>() != null)
                        {
                            k              = keyholesInRange[i].GetComponent <Keyhole>();
                            k.keysInRange -= 1;

                            if (k.keysInRange <= 0)
                            {
                                k.Visual.enabled = false;
                            }
                        }
                    }
                }
            }
        }
    }