Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Changing FirstPerson Mouse Sensitivity thru a Main Menu/Settings

Discussion in 'Scripting' started by AnimationSB, Apr 12, 2021.

  1. AnimationSB

    AnimationSB

    Joined:
    Mar 8, 2021
    Posts:
    22
    I am currently working on a First Person walkaround experience for a random house. The idea is similar to Oculus VR experiences, but as a windows application.

    So far, I have a working main menu scene with a mouse sensitivity option.
    upload_2021-4-12_9-20-55.png
    upload_2021-4-12_9-20-34.png

    Clicking start will load the main House scene. (scene + 1 in the build settings)

    I am a beginner in C++ coding and am a bit lost right now. My OptionsMenu game object has this script and I have already linked it to my Slider object. Thru testing, I know that this part works. Variable "slider" gets the value of the slider, and the debug log shows me its value. This part is all clear

    The "mouseSensitivity" is suppose to take that value, but keep it for the next scene. This is where I am stuck.
    upload_2021-4-12_9-22-41.png
    upload_2021-4-12_9-23-0.png

    This is my current WIP MouseLook script, based on a tutorial I watched online. Basically it takes the horizontal and vertical mouse input and turns it into movement for the camera. This works 100%

    the mouseSensitivity here is based on the tutorial's example.

    (The code for the ESC key is something I made myself. It returns to the main menu (scene 0) when pressed.)

    The concept is, instead of declaring mouseSensitivity as 10f here, I want it to take the value of mouseSensitivity set in the main menu slider. How do I do this?
    upload_2021-4-12_9-38-25.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Generally this can be accomplished with some type of persistent state object, such as a GameManager.

    You could just make a static class and do it that way. Here's a super-simple example:

    Here's an example of simple persistent loading/saving values using PlayerPrefs:

    https://pastebin.com/icJSq5zC

    Useful for a relatively small number of simple values.

    Another way, if your state might need regular updating, is to implement a singleton.

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://pastebin.com/SuvBWCpJ

    Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

    https://pastebin.com/cv1vtS6G

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

    If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
     
    AnimationSB likes this.
  3. AnimationSB

    AnimationSB

    Joined:
    Mar 8, 2021
    Posts:
    22
    Thanks. I figured it out. Deleted my previous reply. Sharing my solution here for everyone else.

    I used the first option for the mean time. It works. I still got to understand and familiarize myself with all the new classes and terms, but I just replaced the different variables.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Settings
    6. {
    7.     private static string s_MSLevel = "MSLevel";
    8.     public static float MSLevel
    9.     {
    10.         get
    11.         {
    12.             return PlayerPrefs.GetFloat(s_MSLevel, 1);
    13.         }
    14.         set
    15.         {
    16.             PlayerPrefs.SetFloat(s_MSLevel, value);
    17.         }
    18.     }
    19. }
    Settings.MSLevel will work for both my scripts in all my scenes.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SettingsMenu : MonoBehaviour
    6. {
    7.     public void SetSensitivity(float slider)
    8.     {
    9.         Debug.Log(slider);
    10.         Settings.MSLevel = slider;
    11.     }
    12.  
    13. }