private void UpdateMovement()
    {
        if (_controller.Collisions.Above || _controller.Collisions.Below)
        {
            Velocity.y = 0;
        }

        var input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

        Debug.DrawRay(transform.position, input, Color.yellow);

        if (Input.GetButtonDown("Jump") && _controller.Collisions.Below)
        {
            Velocity.y = _maxJumpVelocity;
        }

        if (Input.GetButtonUp("Jump"))
        {
            if (Velocity.y > _minJumpVelocity)
            {
                Velocity.y = _minJumpVelocity;
            }
        }

        var targetVelocityX = Mathf.Round(input.x) * _moveSpeed;

        Velocity.x = Mathf.SmoothDamp(Velocity.x, targetVelocityX, ref _velocityXSmoothing,
                                      _controller.Collisions.Below ? _accelerationTimeGrounded : _accelerationTimeAirborne);

        Velocity.y += Constants.GRAVITY * Time.deltaTime;

        _controller.Move(Velocity * Time.deltaTime);
    }
Beispiel #2
0
    protected virtual void UpdateMovement()
    {
        var moveDirection = Mathf.Sign(moveSpeed);


        if (!_moveVertical)
        {
            GetComponent <SpriteRenderer>().flipX = moveDirection == 1;            // moving right

            if (_controller.collisions.left || _controller.collisions.right)
            {
                moveSpeed = -moveSpeed;
            }
            if (_controller.collisions.above || _controller.collisions.below)
            {
                _velocity.y = 0;
            }

            _velocity.x = moveSpeed;
        }

        else
        {
            if (_controller.collisions.above || _controller.collisions.below)
            {
                moveSpeed = -moveSpeed;
            }

            if (_controller.collisions.left || _controller.collisions.right)
            {
                moveSpeed = -moveSpeed;
            }


            _velocity.y = moveSpeed;
        }


        if (Vector3.Distance(transform.position, PathMiddlePos) > _unitsToMove && _unitsToMove > 0 && !_justTurnedAround)
        {
            moveSpeed = -moveSpeed;
            StartCoroutine(TurnAround());
        }

        //	var movingDirection = (_movetarget - transform.position).normalized;

        //	_velocity += moveSpeed * movingDirection * Time.deltaTime;

        if (useGravity)
        {
            _velocity.y += Physics2D.gravity.x * Time.deltaTime;
        }

        _controller.Move(_velocity * Time.deltaTime);
    }
Beispiel #3
0
    private void UpdateMovement()
    {
        _hasMovedThisFrame = false;

        if (_controller.collisions.above || _controller.collisions.below)
        {
            _velocity.y = 0;
            _hasJumped  = false;
            _animatorController.SetTrigger("TouchGround");
        }

        var input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

        if (input != Vector2.zero)
        {
            _hasMovedThisFrame = true;
        }

        Debug.DrawRay(transform.position, input, Color.red);

        if (Input.GetButtonDown("Jump") && _controller.collisions.below)
        {
            AudioManager.Instance.Play("Jumping");
            _animatorController.SetTrigger("Jump");
            _velocity.y = _maxJumpVelocity;
            _hasJumped  = true;
        }

        if (Input.GetButtonUp("Jump") && !_hasJumped)
        {
            if (_velocity.y > _minJumpVelocity)
            {
                _velocity.y = _minJumpVelocity;
            }
        }

        var targetVelocityX = input.x * moveSpeed;

        _velocity.x = Mathf.SmoothDamp(_velocity.x, targetVelocityX, ref _velocityXSmoothing, (_controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne);

        _velocity.y += Physics2D.gravity.x * Time.deltaTime;

        _animatorController.SetBool("Walking", _hasMovedThisFrame);

        _controller.Move(_velocity * Time.deltaTime);
    }
Beispiel #4
0
    private void UpdateMovement()
    {
        if (_controller.collisions.above || _controller.collisions.below)
        {
            _velocity.y = 0;
        }

        if (_controller.collisions.left || _controller.collisions.right)
        {
            if (!_justTurnedAround)
            {
                moveSpeed = -moveSpeed;
                GetComponent <SpriteRenderer>().flipX = !GetComponent <SpriteRenderer>().flipX;
                StartCoroutine(TurnAround());
            }
        }

        _velocity.x = moveSpeed;

        _velocity.y += Physics2D.gravity.x * Time.deltaTime;

        _controller.Move(_velocity * Time.deltaTime);
    }
Beispiel #5
0
    void UpdateMovement()
    {
        if (!_justCollided)
        {
            // reflect
            if (_controller.collisions.left)
            {
                _velocity = Vector3.Reflect(_velocity, Vector3.right);
                StartCoroutine(Collided());
            }
            else if (_controller.collisions.right)
            {
                _velocity = Vector3.Reflect(_velocity, Vector3.left);
                StartCoroutine(Collided());
            }
            else if (_controller.collisions.above)
            {
                _velocity = Vector3.Reflect(_velocity, Vector3.down);
                StartCoroutine(Collided());
            }
            else if (_controller.collisions.below)
            {
                _velocity = Vector3.Reflect(_velocity, Vector3.up);
                StartCoroutine(Collided());
            }
        }



        if (_useGravity)
        {
            _velocity.y += Physics2D.gravity.x * Time.deltaTime;
        }

        _controller.Move(_velocity * Time.deltaTime);
    }