Unityでマウス感度調整のスライダーをつくる(コミュニティ限定)

ゲームによくあるマウス感度調整です。好みによって早い遅いを調整できるようにします。
意外と簡単だったので、このUnityの標準機能のスライダーは使えそうです。

Unityの標準機能のSliderを出します。
サイズやテキストなどを変更します。Sliderのサイズ調整はMin ValueとMax Valueで調整できます。

using UnityEngine;
using UnityEngine.UI;

public class SensitivitySetting : MonoBehaviour
{
    [Header("マウス感度調整スライダー")]
    [SerializeField] public Slider sensitivitySlider;      // UIのスライダー
    [SerializeField] public ObjectController playerScript; // プレイヤーのスクリプト

    void Start()
    {
        // ゲーム起動時、すでに保存されている感度があればスライダーの初期値にする(なければ 1.0f)
        float savedSensitivity = PlayerPrefs.GetFloat("CursorSensitivity", 1.0f);
        if (sensitivitySlider != null)
        {
            sensitivitySlider.value = savedSensitivity;
        }

        // スライダーが動いたときに、自動で「OnSliderChanged」関数を呼び出す設定
        if (sensitivitySlider != null)
        {
            sensitivitySlider.onValueChanged.AddListener(OnSliderChanged);
        }
    }



    // スライダーの値が変わるたびに「リアルタイム」で呼ばれる関数
    private void OnSliderChanged(float value)
    {
        // その場の値を PlayerPrefs に即時保存
        PlayerPrefs.SetFloat("CursorSensitivity", value);
        // PlayerPrefs.Save();

        // プレイヤーに即時反映
        if (playerScript != null)
        {
            playerScript.LoadSensitivity();
        }
    }

} 

先ほどのScriptをアタッチします。
項目にアタッチします。マウス感度調整、はプレイヤーについているObjectContollerが必要なのでプレイヤーをアタッチします。
またプレイヤーのObjectContollerもScriptも修正が必要です。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


// プレイヤーの操作
public class ObjectController : MonoBehaviour
{
    public float moveSpeed = 3.0f;
    public Vector2 rotationSpeed = new Vector2(10.0f, 10.0f);

    // 基準となる元の感度を保持する変数
    private Vector2 baseRotationSpeed;

    //private Vector2 lastMousePosition;
    private Vector2 newAngle = Vector2.zero;
    private CharacterController controller;
    public SoundRunScript soundRunScript;
    private bool isGameOver = false;
    private Vector3 velocity;
    private float gravity = -9.8f;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        if (controller == null)
            controller = GetComponentInParent<CharacterController>();

        // 元の感度を記憶しておく
        baseRotationSpeed = rotationSpeed;

        // ゲーム起動時に保存された感度を適用する
        LoadSensitivity();
    }

    // 感度を読み込んで適用する関数
    public void LoadSensitivity()
    {
        // 保存された倍率(データがなければ 1.0f)を取得
        float sensitivityMultiplier = PlayerPrefs.GetFloat("CursorSensitivity", 1.0f);

        // 元の感度に倍率をかける
        rotationSpeed = baseRotationSpeed * sensitivityMultiplier;
        Debug.Log($"感度を適用しました: 倍率 {sensitivityMultiplier}");
    }

    void Update()
    {
        if (controller == null) return;
        if (isGameOver) return;

        Vector3 moveDirection = Vector3.zero;
        bool isMoving = false;

        // 前進(Y成分除去で水平移動のみ)
        if (Input.GetKey(KeyCode.X))
        {
            Vector3 forward = transform.forward;
            forward.y = 0;
            moveDirection += forward.normalized;
            isMoving = true;
        }
        // 后退(Y成分除去で水平移動のみ)
        if (Input.GetKey(KeyCode.S))
        {
            Vector3 forward = transform.forward;
            forward.y = 0;
            moveDirection -= forward.normalized;
            isMoving = true;
        }
        if (Input.GetKey(KeyCode.D)) { moveDirection += transform.right; isMoving = true; }
        if (Input.GetKey(KeyCode.A)) { moveDirection -= transform.right; isMoving = true; }

        // 走る音
        if (soundRunScript != null)
            soundRunScript.playSoundRun = isMoving;

        // 重力
        if (controller.isGrounded && velocity.y < 0)
            velocity.y = -2f;
        velocity.y += gravity * Time.deltaTime;

        // 移動適用
        controller.Move((moveDirection.normalized * moveSpeed + velocity) * Time.deltaTime);

        // 視点操作(左クリック中のみ)
        if (Input.GetMouseButton(0))
        {
            float mouseX = Input.GetAxis("Mouse X");
            float mouseY = Input.GetAxis("Mouse Y");

            // rotationSpeed が動的に変わるため、ここの計算にそのまま反映されます
            newAngle.y += mouseX * 6f * rotationSpeed.y;
            newAngle.x -= mouseY * 6f * rotationSpeed.x;
            newAngle.x = Mathf.Clamp(newAngle.x, -80f, 80f);
            transform.localEulerAngles = new Vector3(newAngle.x, newAngle.y, 0);
        }
    }

    public void StopPlayer()
    {
        isGameOver = true;
        Debug.Log("プレイヤーの操作を停止しました");
    }
}


これでマウス感度をスライダーで調整できます。またそのスライダーで調整したものは、移動した瞬間に反映されるようになっています。