Unity 3d库存脚本(拾取对象后,再次表示拾取的pres e)



我正在为对象创建库存,当我拾取对象时,它存储在库存中,但我的显示行(按E拾取)仍显示。方法ONGUI,我想制造一些问题,这是FPS拾取的代码。

#pragma strict
var InstructionBoxSkin : GUISkin; 
var ButtonToPress : KeyCode = KeyCode.E; 
var PickUpDistance = 1.7f;
private var canPickUp = false;
private var theItem : Item;
private var thePlayer : Transform;
private var dist = 9999f;
@script AddComponentMenu ("Inventory/Items/First Person Pick Up")
@script RequireComponent(Item)
function Awake ()
{
    theItem = (GetComponent(Item));
    if (InstructionBoxSkin == null)
    {
        InstructionBoxSkin = Resources.Load("OtherSkin", GUISkin);
    }
}
function RetrievePlayer (theInv : Inventory)
{
    thePlayer = theInv.transform.parent;
}
function OnGUI ()
{
    //This is where we draw a box telling the Player how to pick up the item.
    //
    GUI.skin = InstructionBoxSkin;
    GUI.color = Color(1, 1, 1, 0.7);
    if (canPickUp == true)
    {
        if (transform.name.Length <= 1)
        {
            GUI.Box (Rect (Screen.width*0.5-(165*0.5), 200, 165, 22), "Press E to pick up " + transform.name + ".");
        }
        else
        {
           GUI.Box (Rect (Screen.width*0.5-(185*0.5), 200, 185, 22), "Press E to pick up " + transform.name + ".");
        }
    }
}
function Update ()
{
    if (thePlayer != null)
    {
        dist = Vector3.Distance(thePlayer.position, transform.position);
        if (dist <= PickUpDistance)
        {
            canPickUp = true;
        }
        else
        {
            canPickUp = false;
        }
        //This is where we allow the player to press the ButtonToPress to pick up the item.
        if (Input.GetKeyDown(ButtonToPress) && canPickUp == true)
        {
            theItem.PickUpItem();
        }
    }
}
function OnDrawGizmosSelected () 
{
    Gizmos.color = Color.yellow;
    Gizmos.DrawWireSphere (transform.position, PickUpDistance);
}

原因可能是什么?

看起来canPickUp从未设置为false。

是否更改:

    if (Input.GetKeyDown(ButtonToPress) && canPickUp == true)
    {
        theItem.PickUpItem();
        canPickUp = false;
    }

解决问题?

最新更新