Ejemplo n.º 1
0
    /// <summary>
    /// The current held object is dropped.
    /// </summary>
    /// <param name="ac">The player's action controller</param>
    void Drop(PlayerActionController ac)
    {
        // If it was held in hand
        if (canBeHeld)
        {
            this.gameObject.SetActive(true);
            float dropBackOffset = 1f;
            this.transform.position = ac.transform.position + ac.transform.forward * (-dropBackOffset);
            float castDistance = 1f;

            // AC pos
            //Debug.Log("Check dropping: AC position " + ac.transform.position);
            //Debug.DrawRay(ac.transform.position, Vector3.up, Color.red, 2.0f);

            // FROM pos
            //Debug.Log("Check dropping: FROM position " + this.transform.position);
            //Debug.DrawRay(this.transform.position, Vector3.up, Color.blue, 2.0f);

            // RAYCAST
            //Debug.Log("Check dropping: FROM position " + this.transform.position);
            //Debug.DrawRay(this.transform.position, ac.transform.forward * castDistance, Color.magenta, 2.0f);

            // Check if we can drop it here
            RaycastHit hitInfo;
            LayerMask mask = 1 << LayerMask.NameToLayer("PlayerTrigger");
            mask = ~mask;
            if (!Physics.Raycast(this.transform.position, ac.transform.forward, out hitInfo, castDistance, mask))
            {
                //Debug.Log("We can drop it!");
                // Can drop it here, drop it with a velocity impulse
                this.transform.position += ac.transform.forward * dropBackOffset / 2; // We move it a bit farther
                this.rigidbody.AddForce(ac.transform.forward * DROP_IMPULSE, ForceMode.VelocityChange);
            }
            else
            {
                //Debug.Log("We cannot drop it! Leave it just in front of us! We hit a " + hitInfo.collider.gameObject.name);
                // Cannot drop it here, just drop it without an impulse
                float feetDistance = 0.5f;
                float forwardOffset = 0.45f;

                this.transform.position = Statics.instance.playerMovementController.transform.position;
                this.transform.position += Vector3.down * feetDistance; // A bit downfloat forwardOffset = 0.1f;
                this.transform.position += Statics.instance.playerMovementController.transform.forward * forwardOffset; // A bit forward
                //Debug.DrawRay(this.transform.position, Vector3.up, Color.black, 2.0f);
            }
        }

        // If it was dragged or picked up
        else if (canBeDragged || canBePickedUp)
        {
            this.physicsCollider.gameObject.layer = LayerMask.NameToLayer("Default");

            // Just drop it
            var posAttractor = GetComponent<PositionAttractor>();
            posAttractor.enabled = false;
            var rotAttractor = GetComponent<OrientationAttractor>();
            rotAttractor.enabled = false;
        }

        // Reset player movement modifier settings
        Statics.instance.playerMovementController.ResetSpeedModifier();
        Statics.instance.playerMovementController.ResetDisableJump();

        this.transform.parent = null;
        this.rigidbody.useGravity = true;

        ac.ClearHeldObject();
    }