Esempio n. 1
0
 private void CheckIfStartedFalling()
 {
     if (_isJumping == false)
     {
         StartFalling?.Invoke();
         Debug.Log("Falling");
     }
 }
    void Update()
    {
        if (!_player.IsDead)
        {
            float   horizontal = Input.GetAxisRaw("Horizontal");
            float   vertical   = Input.GetAxisRaw("Vertical");
            Vector3 direction  = new Vector3(horizontal, 0f, vertical).normalized;
            Vector3 moveDir    = Vector3.zero;

            if (direction.magnitude >= 0.1f && _player.CanMove)
            {
                float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
                float angle       = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
                transform.rotation = Quaternion.Euler(0f, angle, 0f);

                moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;

                if (Input.GetKey(KeyCode.LeftShift))
                {
                    if (!_isSprinting && !_takingDamage && controller.isGrounded)
                    {
                        StartSprinting?.Invoke();
                        _dashParticles.enableEmission = true;
                        _isSprinting = true;
                        StartCoroutine(SprintSteps());
                    }
                    _isRunning = false;
                }
                else
                {
                    CheckIfStartedMoving();
                    _isSprinting = false;
                }
            }
            else
            {
                CheckIfStoppedMoving();
            }

            if (Input.GetKeyDown(KeyCode.Space) && controller.isGrounded && _player.CanMove)
            {
                Jump?.Invoke();
                _isRunning   = false;
                _isSprinting = false;
                yVelocity.y  = 0;
                yVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravity);
            }

            if (!controller.isGrounded)
            {
                if (yVelocity.y < 0 && !_isFalling)
                {
                    StartFalling?.Invoke();
                    _isFalling = true;
                }
            }

            if (_isFalling && controller.isGrounded)
            {
                _landingParticles.Emit(30);
                _landSound.PlayOneShot(_landSound.clip);
                if (!_isRunning && !_isSprinting)
                {
                    Land?.Invoke();
                }
                _isFalling = false;
            }

            if (!_isSprinting)
            {
                _dashParticles.enableEmission = false;
            }

            yVelocity.y += gravity * Time.deltaTime;
            moveDir     += yVelocity;
            moveDir.x   *= speed;
            moveDir.z   *= speed;

            if (Input.GetKey(KeyCode.LeftShift))
            {
                _speedBoost = true;
            }
            else
            {
                _speedBoost = false;
            }

            if (_speedBoost)
            {
                moveDir.x *= sprintSpeed;
                moveDir.z *= sprintSpeed;
            }
            controller.Move(moveDir * Time.deltaTime);
        }

        if (_player.IsDead)
        {
            _dashParticles.enableEmission = false;
        }
    }
Esempio n. 3
0
    // Update is called once per frame
    void Update()
    {
        // Movement
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical   = Input.GetAxisRaw("Vertical");

        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;

        if (!_isAirborne && _isMoving)
        {
            if (Input.GetKeyDown(KeyCode.LeftShift) && !_isSprinting)
            {
                _isSprinting = true;
                StartSprinting?.Invoke();
            }
        }
        if (Input.GetKeyUp(KeyCode.LeftShift) && _isSprinting)
        {
            _isSprinting = false;
            if (!_isAirborne && _isMoving)
            {
                StartRunning?.Invoke();
            }
        }

        if (controller.isGrounded)
        {
            _vSpeed = 0f;
            if (Input.GetKeyDown("space"))
            {
                Jump?.Invoke();
                _vSpeed     = jumpSpeed;
                _isAirborne = true;
            }
        }
        else
        {
            if (!_playerCharacterAnimator.IsPlayingAny())
            {
                if (!_isFalling)
                {
                    StartFalling?.Invoke();
                    Debug.Log("Started Falling");
                    _isFalling = true;
                }
            }
        }

        if (direction.magnitude >= .1f || _vSpeed != 0)
        {
            CheckIfStartedMoving();
            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle       = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, 0f);

            Vector3 moveDir = new Vector3();
            if (direction.magnitude >= .1f)
            {
                moveDir = (Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward).normalized;
            }

            if (!_isSprinting)
            {
                moveDir *= speed;
            }
            else
            {
                moveDir *= sprintSpeed;
            }

            _vSpeed  -= _gravity * Time.deltaTime;
            moveDir.y = _vSpeed;
            controller.Move(moveDir * Time.deltaTime);

            CheckIfLanded();
        }
        else
        {
            CheckIfStoppedMoving();
        }
    }