示例#1
0
    private void FixedUpdate()
    {
        var isGrounded    = _characterGrounding.isGrounded;
        var groundMovedBy = _characterGrounding.groundMovedBy;
        var currentSpeed  = DWInput.GetAxis("Horizontal") * speedFactor;

        NormalizedMovementSpeed = Mathf.Abs(currentSpeed / speedFactor);

        // move with platform
        var jumpXVelocity = 0f;

        if (groundMovedBy.HasValue)
        {
            // _rigidBody2D.MovePosition(_rigidBody2D.position + groundMovedBy.Value);
            Vector3 moveBy = groundMovedBy.Value;
            transform.position += moveBy;
            jumpXVelocity       = (moveBy.x / Time.fixedDeltaTime) * _rigidBody2D.mass * jumpXInertia;
        }

        // move
        var movementVector = new Vector3(currentSpeed * Time.fixedDeltaTime, 0f, 0f);

        transform.position += movementVector;

        // orient
        if (!Mathf.Approximately(currentSpeed, 0f))
        {
            _spriteRenderer.flipX = currentSpeed > 0f;
        }

        // jump
        if (_shouldDoJump && isGrounded)
        {
            // it is safe to assume that if the character is grounded it also has a groundingDirection
            var force = new Vector2(jumpXVelocity, 1f * jumpForce) - _characterGrounding.groundingDirection.Value * reboundForce;
            _rigidBody2D.AddForce(force);
            audioSource.InstantPlay(jumpClip);
        }
        _shouldDoJump = false;
    }