|  | 
 
| 设置玩家通关需要的钥匙的逻辑   KeyPickup
 C#
 JS复制代码using UnityEngine;
using System.Collections;
public class KeyPickup : MonoBehaviour
{
    public AudioClip keyGrab;                       // Audioclip to play when the key is picked up.
    
    
    private GameObject player;                      // Reference to the player.
    private PlayerInventory playerInventory;        // Reference to the player's inventory.
    
    
    void Awake ()
    {
        // Setting up the references.
        player = GameObject.FindGameObjectWithTag(Tags.player);
        playerInventory = player.GetComponent<PlayerInventory>();
    }
    
    
    void OnTriggerEnter (Collider other)
    {
        // If the colliding gameobject is the player...
        if(other.gameObject == player)
        {
            // ... play the clip at the position of the key...
            AudioSource.PlayClipAtPoint(keyGrab, transform.position);
            
            // ... the player has a key ...
            playerInventory.hasKey = true;
            
            // ... and destroy this gameobject.
            Destroy(gameObject);
        }
    }
}
复制代码#pragma strict
public var keyGrab : AudioClip;                         // Audioclip to play when the key is picked up.
private var player : GameObject;                        // Reference to the player.
private var playerInventory : PlayerInventory;      // Reference to the player's inventory.
function Awake ()
{
    // Setting up the references.
    player = GameObject.FindGameObjectWithTag(Tags.player);
    playerInventory = player.GetComponent(PlayerInventory);
}
function OnTriggerEnter (other : Collider)
{
    // If the colliding gameobject is the player...
    if(other.gameObject == player)
    {
        // ... play the clip at the position of the key...
        AudioSource.PlayClipAtPoint(keyGrab, transform.position);
        
        // ... the player has a key ...
        playerInventory.hasKey = true;
        
        // ... and destroy this gameobject.
        Destroy(gameObject);
    }
}
 | 
 |