private void Move()
    {
        float moveVertical   = Input.GetAxisRaw("Vertical");
        float moveHorizontal = Input.GetAxisRaw("Horizontal");

        direction           = new Vector3(moveHorizontal, 0.0f, moveVertical);
        normalizedDirection = direction.normalized;

        float targetAngle = Mathf.Atan2(normalizedDirection.x, normalizedDirection.z) * Mathf.Rad2Deg + cam.transform.eulerAngles.y;

        moveDirection = Quaternion.Euler(0.0f, targetAngle, 0.0f) * Vector3.forward;

        isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);

        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        if (isGrounded && !playerAim.GetIsAiming() && !playerRegularAttack.GetIsRegularAttack())
        {
            if (direction == Vector3.zero)
            {
                Idle();
            }
            else
            {
                if (Input.GetKey(KeyCode.LeftShift))
                {
                    Run();
                }
                else
                {
                    Walk();
                }
            }
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Jump();
            }
        }

        //Gravity
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }
Exemple #2
0
    private void Move()
    {
        float moveVertical   = Input.GetAxisRaw("Vertical");
        float moveHorizontal = Input.GetAxisRaw("Horizontal");

        direction           = new Vector3(moveHorizontal, 0.0f, moveVertical);
        normalizedDirection = direction.normalized;

        float targetAngle = Mathf.Atan2(normalizedDirection.x, normalizedDirection.z) * Mathf.Rad2Deg + cam.transform.eulerAngles.y;

        moveDirection = Quaternion.Euler(0.0f, targetAngle, 0.0f) * Vector3.forward;

        if (!playerAim.GetIsAiming() && !playerRegularAttack.GetIsRegularAttack())
        {
            if (direction == Vector3.zero)
            {
                Idle();
            }
            else
            {
                if (Input.GetKey(KeyCode.LeftShift))
                {
                    Run();
                }
                else
                {
                    Walk();
                }
            }

            //Orientation
            if (direction != Vector3.zero)
            {
                playerBody.transform.rotation = Quaternion.Slerp(playerBody.transform.rotation, Quaternion.LookRotation(normalizedDirection), Time.deltaTime * turnSpeed);
            }
        }
    }