【问题标题】:Unity 3d Inventory script (after picking object, again saying pres e for pick)Unity 3d Inventory 脚本(拾取对象后,再次说 pres e for pick)
【发布时间】:2016-03-13 12:42:11
【问题描述】:

我正在为对象创建一个库存,当我选择对象时它存储在库存中,但我的显示行​​(按 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);
}

可能是什么原因?

【问题讨论】:

    标签: javascript unity3d scripting inventory


    【解决方案1】:

    看起来 canPickUp 从未设置为 false。

    变化:

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

    解决问题?

    【讨论】:

    • 我尝试将其设置为 false,但后来它不起作用@Kaos_nyrb
    猜你喜欢
    • 1970-01-01
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多