示例#1
0
    //To pull an item from your inventory to use or of the ground using tags
    public void UseItem(string itemTag, string targetTag)
    {
        if (!inventory.HaveItem(this.name))
        {
            //Debug.Log("HIT return");
            //return;
            //pos = this.transform.position;
        }

        //To store the mouses position
        Vector2 locationOfMouse = Input.mousePosition;

        //Grab vector2 for cursor to use in AABB math
        cursorPosition = Input.mousePosition;
        cursorPosition = Camera.main.ScreenToWorldPoint(cursorPosition);

        //To get an item with the desired tag
        GameObject taggedItem;

        taggedItem = GameObject.FindWithTag(targetTag);

        //Selection for objects
        if (Input.GetMouseButton(1) && !this.frozen && !this.talking)
        {
            //AABB collision test for cursor
            if (cursorPosition.x < this.GetComponent <BoxCollider2D>().bounds.max.x&& cursorPosition.x > this.GetComponent <BoxCollider2D>().bounds.min.x)
            {
                //Potential collision!
                //Check the next condition in a nested if statement, just to not have a ton of &'s and to be more efficient
                if (cursorPosition.y > this.GetComponent <BoxCollider2D>().bounds.min.y&& cursorPosition.y < this.GetComponent <BoxCollider2D>().bounds.max.y)
                {
                    //Collision!
                    this.transform.position = cursorPosition;

                    //to see if the item is near the target zone
                    if (Vector2.Distance(this.transform.position, taggedItem.transform.position) <= 0.2f)
                    {
                        //Validating item
                        if (this.tag != itemTag)
                        {
                            Debug.Log("HIT RETURN: " + this.tag + " " + itemTag);
                            return;
                        }
                        //Do something with the target zone and/or the object in use
                        HitZone(itemTag);
                        if (name == "Badge")
                        {
                            inventory.AddBadge();
                        }
                        added = false;
                        inventory.RemoveItem(this);
                    }
                }
            }
        }
    }