首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unity 3d库存脚本(拾取对象后,再次说pres表示拾取)

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

Stack Overflow用户
提问于 2015-12-08 22:33:17
回答 1查看 658关注 0票数 0

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

代码语言:javascript
复制
#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);
}

可能的原因是什么?

EN

回答 1

Stack Overflow用户

发布于 2015-12-09 00:29:18

看起来canPickUp从未设置为false。

是否进行更改:

代码语言:javascript
复制
    if (Input.GetKeyDown(ButtonToPress) && canPickUp == true)
    {
        theItem.PickUpItem();
        canPickUp = false;
    }

解决问题了吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34158534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档