Example #1
0
        /// <summary>
        /// Throw the grabbable if needed.
        /// </summary>
        private void Throwing()
        {
            if (Time.time - _throwingTime < 0.1f)
            {
                return;
            }

            _throwingTime = Time.time;
            _isThrowing   = true;

            if (_hasRigidbody)
            {
                Vector3 velocity = GetVelocity();

                _rigidbody.isKinematic = _initKinematic;
                _rigidbody.velocity    = velocity * _powerScale;
                _lastVelocity          = _rigidbody.velocity;

                if (velocity.sqrMagnitude >= _throwVelocityThreshold)
                {
                    OnThrown?.Invoke(_grabbable);
                }
            }

            _prevPos      = Vector3.zero;
            _velocityData = new Vector3[_velocityData.Length];
        }
Example #2
0
    /**
     * If this object is currently being carried by a player,
     * throw it in the given direction.
     *
     * @param direction direction into which this object shall be thrown.
     *                  You can use `Vector2.zero` to just drop it.
     *                  You can use `Carrier.ThrowDirection` to throw it in the
     *                  direction the carrying player is currently walking into.
     */
    public void Throw(Vector2 direction)
    {
        if (IsBeingCarried)
        {
            this.gameObject.layer            = LayerMask.NameToLayer("Thrown");
            _physicsEffects.rigidbody2D.mass = _cachedMass;
            _physicsEffects.FrictionEnabled  = false;

            ApplyThrowingMovement(direction);

            _hingeJoint2D.connectedBody = null;
            _hingeJoint2D.enabled       = false;

            IsBeingCarried = false;
            _carrier.ExitCarryingState();
            _carrier.Input.EnableButtons((Button.Throw, false));
            OnThrown?.Invoke();
        }

        else
        {
            Debug.LogError("Tried to throw object which is not being carried!");
        }
    }