コード例 #1
0
        private void Movement()
        {
            // TODO: fully convert to StatAttribute
            // get the actual speed with all modificators
            float speed = playerProperties.speed.Value;

            if (isRunning)
            {
                speed *= playerProperties.runMultiplier;
            }
            if (isSneaking)
            {
                speed *= playerProperties.sneakMultiplier;
            }

            // get the inputs
            float horizontal = Input.GetAxis("Horizontal");
            float vertical   = Input.GetAxis("Vertical");

            // makes sure that sideway walking is slower than forward walking
            if (vertical < -0.01)
            {
                speed *= 0.7f;
            }

            Vector3 velocity = ((transform.forward * vertical) + (transform.right * horizontal));

            if (CheckMoveableTerrain(
                    new Vector3(playerCameraTransform.position.x, playerCameraTransform.position.y - 1.7f,
                                playerCameraTransform.position.z), new Vector3(velocity.x, 0, velocity.z), 5f))
            {
                // makes sure, that the total veloctity is not higher while walking cross-ways
                if (velocity.magnitude > 1.01)
                {
                    float ySaver = velocity.y;
                    velocity.y = 0;
                    velocity   = velocity.normalized;
                    velocity.y = ySaver;
                }

                // manages movement depending on being airborne or not
                if (isAirborne == 0)
                {
                    velocity          *= speed;
                    velocity.y         = rigidbody.velocity.y;
                    rigidbody.velocity = velocity;
                }
                else
                {
                    velocity  *= speed;
                    velocity.y = 0;

                    rigidbody.AddForce(velocity, ForceMode.Impulse);

                    // make sure, that the player is not able to be faster then the momentarily speed level is allowing him to be
                    velocity   = rigidbody.velocity;
                    velocity.y = 0;
                    velocity   = velocity.normalized * Mathf.Clamp(velocity.magnitude, 0, speed);
                    velocity.y = rigidbody.velocity.y;

                    rigidbody.velocity = velocity;
                }
            }
            else
            {
                rigidbody.velocity =
                    new Vector3(0f, 0f, 0f); // stops the player at an instant if the terrain is not movable
            }

            if (isRunning && velocity.magnitude > 0.1f && isAirborne == 0)
            {
                _characterSounds.Running(groundDetector.GroundType);
            }
            else if (isSneaking && velocity.magnitude > 0.1f && isAirborne == 0)
            {
                _characterSounds.Sneaking(groundDetector.GroundType);
            }
            //TODO: replace with isWalking flag
            else if (isAirborne == 0 && velocity.magnitude > 0.1f)
            {
                _characterSounds.Walking(groundDetector.GroundType);
            }
            else
            {
                _characterSounds.StopMovement();
            }
        }