Пример #1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        _velocity.x *= _velFriction;

        if (_velocity.sqrMagnitude < float.Epsilon)
        {
            _velocity = Vector3.zero;
        }

        _velocity.y -= _gravity * Time.fixedDeltaTime;


        // Vertical cap
        if (_velocity.y > 14.0f)
        {
            _velocity.y = 14.0f;
        }
        if (_velocity.y < -14.0f)
        {
            _velocity.y = -14.0f;
        }

        Vector2 oldPos = transform.position;

        //PERFORM MOVEMENT
        Controller2D.Flags flags = controller.move(_velocity, Time.fixedDeltaTime);
        if (flags.below)
        {
            _velocity.y = 0;
        }

        if (flags.sides)
        {
            _velocity.x = 0;
        }
        if (flags.above)
        {
            _velocity.y = 0;
        }

        Vector2 newPos = transform.position;

        if (oldPos == newPos)
        {
            _velocity = Vector3.zero;
        }
    }
Пример #2
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (inputmovement != Vector3.zero)
        {
            float horizontalMomentum = inputmovement.x * _acceleration;
            _velocity.x += horizontalMomentum;
        }

        //if needed, perform jump
        jump(Time.fixedDeltaTime);
        _velocity.x *= _velFriction;

        if (_velocity.sqrMagnitude < float.Epsilon)
        {
            _velocity = Vector3.zero;
        }

        _velocity.y -= _gravity * Time.fixedDeltaTime;


        // Vertical cap
        if (_velocity.y > 14.0f)
        {
            _velocity.y = 14.0f;
        }
        if (_velocity.y < -14.0f)
        {
            _velocity.y = -14.0f;
        }

        Vector2 oldPos = transform.position;

        //PERFORM MOVEMENT
        Controller2D.Flags flags = controller.move(_velocity, Time.fixedDeltaTime);

        if (animator != null)
        {
            animator.SetFloat("velocity", Mathf.Abs(_velocity.x));
            if (_velocity.x > 0)
            {
                spriteRenderer.flipX = false;
            }
            else if (_velocity.x < 0)
            {
                spriteRenderer.flipX = true;
            }
        }

        if (flags.below)
        {
            _velocity.y = 0;
            if (!_isTouchingGround)
            {
                audioSource.PlayOneShot(contact);
            }
            _isTouchingGround = true;
        }
        else
        {
            _isTouchingGround = false;
        }

        if (flags.sides)
        {
            _velocity.x = 0;
        }
        if (flags.above)
        {
            _velocity.y = 0;
            if (!_isTouchingCeiling)
            {
                audioSource.PlayOneShot(contact);
            }
            _isTouchingCeiling = true;
        }
        else
        {
            _isTouchingCeiling = false;
        }

        Vector2 newPos = transform.position;

        if (oldPos == newPos)
        {
            _velocity = Vector3.zero;
        }
    }