Exemple #1
0
    private void FixedUpdate()
    {
        Vector3 currentPosition = transform.position;

        if (_musicManager != null)
        {
            if (!_isDead && !_options.activeSelf)
            {
                _currVolume = _musicManager.GetDefaultVolume();
            }
            if (Mathf.Abs(_musicManager.GetVolume() - _currVolume) > 0.001 && !_options.activeSelf)
            {
                _musicInterpolation += Time.deltaTime * 4;
                _musicManager.SetVolume(Mathf.Lerp(a: _lastVolume, b: _currVolume, t: _musicInterpolation));
            }
            else
            {
                _musicInterpolation = 0;
            }
        }

        _rb.velocity = new Vector2(x: maxSpeed, _rb.velocity[1]);
        if (currentPosition[1] + (_col.size[1] / 2) < -_height && !_isDead)
        {
            Die();
        }
        Vector2 v      = new Vector2(_rb.position[0], _rb.position[1]);
        Vector3 vAngle = new Vector3(0, 0, 0);

        v[0]     += acceleration;
        vAngle[0] = 1;
        // If is on platform:
        if (IsOnPlatform())
        {
            numFlownPlatforms = -1;
        }

        // Check jump
        if (Input.GetKey("space") && isDebug || Input.touchCount > 0 && Input.GetTouch(0).phase != TouchPhase.Ended && !_isStartJump && (_rb.velocity[1] > 0 && _isJumping || -0.001 < _rb.velocity[1] && _rb.velocity[1] < 0.001 && IsOnPlatform()))
        {
            _isJumping = true;
            v[1]      += acceleration;
            vAngle[1]  = 1;
        }



        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended || Input.touchCount == 0)
        {
            _isStartJump = false;
            _isJumping   = false;
        }

        //Checking borders
        if (currentPosition.y + _col.bounds.extents.y >= _height)
        {
            vAngle[1] = -0.3f;
        }
        if (_rb.velocity[0] > maxSpeed)
        {
            vAngle[0] = 0;
        }
        if (!_isPause && !IsOnSide())
        {
            _rb.AddForce(vAngle * acceleration, mode: ForceMode2D.Impulse);                           // The actual movement
        }
        if (IsOnSide())
        {
            _rb.AddForce((new Vector2(0, -1) * (acceleration * maxSpeed) / 2.3f), mode: ForceMode2D.Impulse);
        }
        _lastx2 = _lastX;
        _lastX  = currentPosition.x;
    }