コード例 #1
0
        /// <summary>
        /// Moves the player around when keys are pressed.
        /// </summary>
        private void Move()
        {
            if (!vrCamera)
            {
                Vector2 mouseMovement = InputUtil.GetMouseMovement() * turnSpeed;
                transform.Rotate(-mouseMovement.y, mouseMovement.x, 0);
                Vector3 rotation = transform.eulerAngles;
                rotation.z = 0;
                if (rotation.x > 180)
                {
                    rotation.x = Mathf.Max(rotation.x, 360 - maxPitch);
                }
                else
                {
                    rotation.x = Mathf.Min(rotation.x, maxPitch);
                }
                transform.eulerAngles = rotation;
            }

            Vector3 targetDirection = Vector3.zero;

            targetDirection += Vector3.right * Input.GetAxis("Horizontal");
            targetDirection += Vector3.forward * Input.GetAxis("Vertical");

            float speedScale = Mathf.Min(1, Vector3.Magnitude(targetDirection));

            targetDirection   = RotateFacing(targetDirection);
            targetDirection.y = 0;
            targetDirection.Normalize();
            targetDirection *= speedScale;

            if (noClip)
            {
                if (InputUtil.GetKey(KeyCode.LeftShift, KeyCode.RightShift, KeyCode.PageDown))
                {
                    targetDirection += Vector3.down;
                }
                if (InputUtil.GetKey(KeyCode.Space, KeyCode.PageUp))
                {
                    targetDirection += Vector3.up;
                }
                targetDirection.Normalize();
            }

            targetDirection *= maxSpeed;
            moveDirection    = Vector3.MoveTowards(moveDirection, targetDirection, acceleration);

            if (noClip)
            {
                controller.Move(moveDirection * Time.fixedDeltaTime);
            }
            else
            {
                controller.SimpleMove(moveDirection);
            }
        }