예제 #1
0
    public void FixedUpdate()
    {
        _doJump    = Input.GetAxis(StringsManager.Vertical) > 0;
        _goSideWay = Input.GetAxis(StringsManager.Horizontal);
        _contactsPoller.Update();

        var walks = Mathf.Abs(_goSideWay) > _movingThresh;

        if (walks)
        {
            _characterView.SpriteRenderer.flipX = _goSideWay < 0;
        }

        var newVelocity = 0f;

        if (walks && (_goSideWay > 0 || !_contactsPoller.HasLeftContacts) && (_goSideWay < 0 || !_contactsPoller.HasRightContacts))
        {
            newVelocity = Time.fixedDeltaTime * _settings.WalkSpeedRB * (_goSideWay < 0 ? -1 : 1);
        }

        _characterRigidbody.velocity = _characterRigidbody.velocity.Change(x: newVelocity);

        if (_contactsPoller.IsGrounded && _doJump && Mathf.Abs(_characterRigidbody.velocity.y) <= _jumpThresh)
        {
            _characterRigidbody.AddForce(Vector3.up * _settings.JumpForse);
        }

        // Animations
        if (_contactsPoller.IsGrounded)
        {
            var track = walks ? CharacterState.Run : CharacterState.Idle;
            _spriteAnimator.StartAnimation(_characterView.SpriteRenderer, track, true, _settings.AnimationSpeed);
        }
        else if (Mathf.Abs(_characterRigidbody.velocity.y) > _flyThresh)
        {
            CharacterState state = CharacterState.Idle;

            if (_characterRigidbody.velocity.y > 0)
            {
                state = CharacterState.JumpUp;
            }
            if (_characterRigidbody.velocity.y < 0)
            {
                state = CharacterState.JumpDown;
            }

            _spriteAnimator.StartAnimation(_characterView.SpriteRenderer, state, true, _settings.AnimationSpeed);
        }
    }
예제 #2
0
    public void FixedUpdate()
    {
        _doJump    = Input.GetAxis(_verticalAxisName) > 0;
        _goSideWay = Input.GetAxis(_horizontalAxisName);
        _contactsPoller.Update();

        var walks = Mathf.Abs(_goSideWay) > _movingThresh;

        if (walks)
        {
            _view._spriteRenderer.flipX = _goSideWay < 0;
        }
        var newVelocity = 0f;

        if (walks &&
            (_goSideWay > 0 || !_contactsPoller.HasLeftContacts) &&
            (_goSideWay < 0 || !_contactsPoller.HasRightContacts))
        {
            newVelocity = Time.fixedDeltaTime * _walkSpeed *
                          (_goSideWay < 0 ? -1 : 1);
        }
        _view._rigidbody2D.velocity = _view._rigidbody2D.velocity.Change(
            x: newVelocity);
        if (_contactsPoller.IsGrounded && _doJump &&
            Mathf.Abs(_view._rigidbody2D.velocity.y) <= _jumpThresh)
        {
            _view._rigidbody2D.AddForce(Vector3.up * _jumpForse);
        }

        //animations
        if (_contactsPoller.IsGrounded)
        {
            var track = walks ? AnimState.Run : AnimState.Idle;
            _spriteAnimator.StartAnimation(_view._spriteRenderer, track, true,
                                           _animationsSpeed);
        }
        else if (Mathf.Abs(_view._rigidbody2D.velocity.y) > _flyThresh)
        {
            var track = AnimState.Jump;
            _spriteAnimator.StartAnimation(_view._spriteRenderer, track, true,
                                           _animationsSpeed);
        }
    }