Пример #1
0
    public bool PickUp()
    {
        //Collect every Pickup around
        GameObject[] pickups = GameObject.FindGameObjectsWithTag("Pickup");

        // Find the closest
        float dist = pickUpDistance;

        for (int i = 0; i < pickups.Length; i++)
        {
            float newDist = (transform.position - pickups[i].transform.position).sqrMagnitude;
            if (newDist < dist)
            {
                carriedObject = pickups[i].transform;
                dist          = newDist;

                Vector2 pos = carriedObject.position;
                pos.x = transform.position.x;
                pos.y = transform.position.y + 1.7f;
                carriedObject.position = pos;
            }
        }

        // Check if we found something
        if (carriedObject != null)
        {
            //check if another player had it, in this case, steal it
            Transform pickupParent = carriedObject.parent;
            if (pickupParent != null)
            {
                PickUpAndHold pickupScript = pickupParent.GetComponent <PickUpAndHold>();
                if (pickupScript != null)
                {
                    pickupScript.Drop();
                }
            }

            carriedObject.parent = gameObject.transform;
            // Set to Kinematic so it will move with the Player
            Rigidbody2D rb2d = carriedObject.GetComponent <Rigidbody2D>();
            if (rb2d != null)
            {
                rb2d.GetComponent <Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
            }
            return(true);
        }
        else
        {
            return(false);
        }
    }
    public bool PickUp()
    {
        PickUp("Pickup");

        for (int i = 0; i < pickupTags.Length; i++)
        {
            PickUp(pickupTags[i]);
        }

        // Check if we found something
        if (carriedObject != null)
        {
            //check if another player had it, in this case, steal it
            Transform pickupParent = carriedObject.parent;
            if (pickupParent != null)
            {
                PickUpAndHold pickupScript = pickupParent.GetComponent <PickUpAndHold>();
                if (pickupScript != null)
                {
                    pickupScript.Drop();
                }
            }

            carriedObject.parent = gameObject.transform;
            // Set to Kinematic so it will move with the Player
            Rigidbody2D rb2d = carriedObject.GetComponent <Rigidbody2D>();
            if (rb2d != null)
            {
                rb2d.GetComponent <Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
            }
            return(true);
        }
        else
        {
            return(false);
        }
    }
Пример #3
0
 private void Start()
 {
     pickedUpAndHeld = GetComponent <PickUpAndHold>();
 }