Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Pause Menu Issue

Discussion in 'UGUI & TextMesh Pro' started by SP-Designs, Jun 13, 2016.

  1. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Hello there!I recently downloaded the unity ui asset from the asset store and it comes with an in-game pause menu which opens in game but it does not stop the game completely.I have a fps character in my 3d game and when i press ESC and the menu opens,i can still move my camera around and i can't find where the issue is coming from in the pause menu script.Below is the script from the asset store and i hope you can help me identify the issue :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Pause : MonoBehaviour {
    6.  
    7.  
    8.    private ShowPanels showPanels;             //Reference to the ShowPanels script used to hide and show UI panels
    9.    private bool isPaused;                 //Boolean to check if the game is paused or not
    10.    private StartOptions startScript;  //Reference to the StartButton script
    11.  
    12.    //Awake is called before Start()
    13.    void Awake()
    14.    {
    15.      //Get a component reference to ShowPanels attached to this object, store in showPanels variable
    16.      showPanels = GetComponent<ShowPanels> ();
    17.      //Get a component reference to StartButton attached to this object, store in startScript variable
    18.      startScript = GetComponent<StartOptions> ();
    19.    }
    20.  
    21.    // Update is called once per frame
    22.    void Update () {
    23.  
    24.      //Check if the Cancel button in Input Manager is down this frame (default is Escape key) and that game is not paused, and that we're not in main menu
    25.      if (Input.GetButtonDown ("Cancel") && !isPaused && !startScript.inMainMenu)
    26.      {
    27.        //Call the DoPause function to pause the game
    28.        DoPause();
    29.      }
    30.      //If the button is pressed and the game is paused and not in main menu
    31.      else if (Input.GetButtonDown ("Cancel") && isPaused && !startScript.inMainMenu)
    32.      {
    33.        //Call the UnPause function to unpause the game
    34.        UnPause ();
    35.      }
    36.    
    37.    }
    38.  
    39.  
    40.    public void DoPause()
    41.    {
    42.      //Set isPaused to true
    43.      isPaused = true;
    44.      //Set time.timescale to 0, this will cause animations and physics to stop updating
    45.      Time.timeScale = 0;
    46.      //call the ShowPausePanel function of the ShowPanels script
    47.      showPanels.ShowPausePanel ();
    48.    }
    49.  
    50.  
    51.    public void UnPause()
    52.    {
    53.      //Set isPaused to false
    54.      isPaused = false;
    55.      //Set time.timescale to 1, this will cause animations and physics to continue updating at regular speed
    56.      Time.timeScale = 1;
    57.      //call the HidePausePanel function of the ShowPanels script
    58.      showPanels.HidePausePanel ();
    59.    }
    60.  
    61.  
    62. }
    63.  
     
  2. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    did you put a Debug.Log statement to show if the Time.timeScale is actually 0.

    ex:
    Code (csharp):
    1.  
    2. void Update()
    3. {
    4.    ...
    5.    if( startScript.inMainMenu )
    6.       Debug.Log( "Time.timeScale: " + Time.timeScale );
    7. }
    8.  
     
  3. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Ok it says time.timescale = 1 so obviously something in the code is wrong right?
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    You probably have another script in the scene that's setting Time.timeScale back to 1.
     
  5. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    I do not, i searched the whole project :( ! Is the code in the update function correct though?
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    Yes. If the pause panel appears, this means it also set Time.timeScale to 0. I'm very familiar with these scripts, since they form the foundation of the free Dialogue System Menu Template on the Dialogue System's free extras page.

    Try adding this script to an empty GameObject in your scene: (It's a variation of IzzySoft's.)

    ShowTimeScale
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class ShowTimeScale : MonoBehavior {
    4.     void OnGUI() {
    5.         GUILayout.Label("Time.timeScale: " + Time.timeScale);
    6.     }
    7. }
    Then play the scene. In the upper left, you should see "Time.timeScale: 1".

    When you press Escape (or whatever you've mapped to the "Cancel" input in Edit > Project Settings > Input), it should change to "Time.timeScale: 0".

    If your player can't move while paused but you can still move the camera around, this means the camera script probably isn't taking Time.timeScale into account. If this is the case, you could add these lines to the top of Pause.cs:
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.Events;
    3. using System.Collections;
    4.  
    5. public class Pause : MonoBehaviour {
    6.     public UnityEvent onPause = new UnityEvent();
    7.     public UnityEvent onUnpause = new UnityEvent();
    8.     ...
    At the end of the DoPause() method, add this line:
    Code (csharp):
    1. onPause.Invoke();
    At the end of the UnPause() method, add this line:
    Code (csharp):
    1. onUnpause.Invoke();
    Then inspect the Pause component of the UI GameObject. It should now have event blocks for "On Pause" and "On Unpause". Click the "+" in the lower right of each one to create a new slot. Assign your camera script. In the "On Pause" block, select the "enabled" property and keep the checkbox unticked. In the "On Unpause" block, do the same but tick the checkbox.

    You can also enable and disable other components this way, too.
     
    Last edited: Jun 15, 2016
  7. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184

    I added the first 3 lines of code in an empty game objects and i can see that the time.timescale = 1 but the pause menu does not show up :p LOL!
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    When you press the "Cancel" input button (e.g., Escape key), the Pause script calls the DoPause() method.

    This method sets Time.timeScale=0 and shows the menu. If the menu doesn't show up and Time.timeScale doesn't change to 0, then this method probably isn't being called. This means the Update() method probably isn't detecting that the "Cancel" input button is down.

    1. Are you getting errors in the Console?

    2. Check the "Cancel" input definition (Edit > Project Settings > Input) and find out what key it's mapped to. Then play the game and try pressing that key.
     
  9. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Ok right now i can press Escape and the menu shows up and time.timescale is 0 but i can still move the camera so i guess the fps camera ignores time.timescale?
     
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    Sure sounds like it. If you add the extra code in my previous post, you can use the events to disable the camera script when the pause menu opens and re-enable it when the pause menu closes.
     
  11. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    The UnityEvent part is giving me an error that that namespace could not be found!
    console : Assets/Game Jam Menu Template/Scripts/Pause.cs(6,47): error CS1526: A new expression requires () or [] after type

    I tried addign () or [] after ...= new UnityEvent; but nothing changed
     
    Last edited: Jun 15, 2016
  12. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    Sorry, I forgot to add a line. Under "using UnityEngine;", add this line:
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.Events; //<-- ADD THIS.
    I updated my post above, too.
     
  13. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    I had already added that line at the top of the script by the time you replied by i in your previous post you said : " Then inspect the Pause component of the UI GameObject. It should now have event blocks for "On Pause" and "On Unpause". Click the "+" in the lower right of each one to create a new slot. Assign your camera script. In the "On Pause" block, select the "enabled" property and keep the checkbox unticked. In the "On Unpause" block, do the same but tick the checkbox. "

    however there is no enabled property or checkbox anywhere in those two boxes from what i saw!
     
  14. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    When you click "+", this should add a slot where you can assign a GameObject. It's similar to the Unity UI Button's On Click event shown below, but it will say On Pause instead:



    If you're familiar with Unity UI, you should have an idea of how to assign a GameObject to the slot that was created by clicking "+". This will add a dropdown next to the slot. You can select the camera script and then enabled from the dropdown.
     
  15. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    That's what i am saying, although i know about unity's UI there is no enabled option for the camerascript in the dropdown menu!
     
  16. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    If you assign the camera script's GameObject to the slot, can you select anything else from the dropdown, such as GameObject > SetActive? (Don't actually select SetActive, since this will deactivate the GameObject. I'm just curious if you can select anything at all from the dropdown.)
     
  17. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Nope just string name :( !
     
  18. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    Would you mind posting the entirety of your modified Pause.cs file?
     
  19. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Sure here you go :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.Events;
    5.  
    6. public class Pause : MonoBehaviour {
    7.   // my code
    8.   public UnityEvent onPause = new UnityEvent();
    9.   public UnityEvent onUnPause = new UnityEvent();
    10.  
    11.    private ShowPanels showPanels;             //Reference to the ShowPanels script used to hide and show UI panels
    12.    private bool isPaused;                 //Boolean to check if the game is paused or not
    13.    private StartOptions startScript;  //Reference to the StartButton script
    14.  
    15.    //Awake is called before Start()
    16.    void Awake()
    17.    {
    18.      //Get a component reference to ShowPanels attached to this object, store in showPanels variable
    19.      showPanels = GetComponent<ShowPanels> ();
    20.      //Get a component reference to StartButton attached to this object, store in startScript variable
    21.      startScript = GetComponent<StartOptions> ();
    22.    }
    23.  
    24.    // Update is called once per frame
    25.    void Update () {
    26.  
    27.      //Check if the Cancel button in Input Manager is down this frame (default is Escape key) and that game is not paused, and that we're not in main menu
    28.      if (Input.GetButtonDown ("Cancel") && !isPaused && !startScript.inMainMenu)
    29.      {
    30.        //Call the DoPause function to pause the game
    31.        DoPause();
    32.      }
    33.      //If the button is pressed and the game is paused and not in main menu
    34.      else if (Input.GetButtonDown ("Cancel") && isPaused && !startScript.inMainMenu)
    35.      {
    36.        //Call the UnPause function to unpause the game
    37.        UnPause ();
    38.      }
    39.  
    40.   if (startScript.inMainMenu)
    41.   {
    42.   Debug.Log("Time.timeScale: " + Time.timeScale);
    43.   }
    44.  
    45. }
    46.  
    47.  
    48.    public void DoPause()
    49.    {
    50.      //Set isPaused to true
    51.      isPaused = true;
    52.      //Set time.timescale to 0, this will cause animations and physics to stop updating
    53.      Time.timeScale = 0;
    54.      //call the ShowPausePanel function of the ShowPanels script
    55.      showPanels.ShowPausePanel ();
    56.   //my code
    57.   onPause.Invoke();
    58.    }
    59.  
    60.  
    61.    public void UnPause()
    62.    {
    63.      //Set isPaused to false
    64.      isPaused = false;
    65.      //Set time.timescale to 1, this will cause animations and physics to continue updating at regular speed
    66.      Time.timeScale = 1;
    67.      //call the HidePausePanel function of the ShowPanels script
    68.      showPanels.HidePausePanel ();
    69.   //my code
    70.   onUnPause.Invoke();
    71.    }
    72.  
    73.  
    74. }
    75.  
     
  20. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    I pasted that script verbatim, and it seems to work:


    Since I don't have a copy of your camera control script, in the screenshot above I just chose the enabled property of the Camera component on the GameObject named Main Camera.
     
  21. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Alright i managed to do it but,i press ESC,the menu opens and time.timescale is set to 0 but the camera can still move around!
     
  22. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    Can you post your camera script? If it's part of an asset you bought on the Asset Store, please don't post it; in this case, let me know which asset.
     
  23. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    There are 3 fps camera scripts all in one main camera components so the asset's name in the asset store is : First Person View :)
     
  24. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    First Person View doesn't control movement or mouse look.

    It does have a script FPV_Example_Player that script weapon switching and field of view control (i.e., zooming). If you're using it, assign it to the OnPause and OnUnPause events.

    Look for something like a Mouse Look component on your player and assign it to the OnPause and OnUnPause events, too. You'll probably need to find whatever components the player uses to move with WASD keys and/or gamepad and add those, too.

    In other words, you need to find the right components to disable, and assign those to the events.
     
  25. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    There are 2 scripts : FirstPersonController and MouseLook that are handling the WASD movement and the Camera Movement but they do not have the enabled property and i guess that's because they are scripts and not cameras?
     
  26. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    In your OnPause event, did you assign the GameObject in your scene that has the FirstPersonController script? If so, then click the dropdown that starts with the value "No Function". Select FirstPersonController > bool enabled. And do similarly for MouseLook.
     
  27. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    The FirstPersonController script handles mouselook on it's own so there is no MouseLookScript.
    So i assigned the FPC script in the OnPause and UnPause event and chose the eanbled bool and checked the box on the UnPause event.Still did not work though,the camera can still look around!
     
  28. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    Can you post the FirstPersonController script here? If it's a paid Asset Store product, don't post it. But if it's a Unity asset (e.g., in the Standard Assets folder) or open source, you can post it.
     
  29. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Code (csharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityStandardAssets.CrossPlatformInput;
    5. using UnityStandardAssets.Utility;
    6. using Random = UnityEngine.Random;
    7.  
    8. namespace UnityStandardAssets.Characters.FirstPerson
    9. {
    10.   [RequireComponent(typeof (CharacterController))]
    11.   [RequireComponent(typeof (AudioSource))]
    12.   public class FirstPersonController : MonoBehaviour
    13.   {
    14.   [SerializeField] private bool m_IsWalking;
    15.   [SerializeField] private float m_WalkSpeed;
    16.   [SerializeField] private float m_RunSpeed;
    17.   [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
    18.   [SerializeField] private float m_JumpSpeed;
    19.   [SerializeField] private float m_StickToGroundForce;
    20.   [SerializeField] private float m_GravityMultiplier;
    21.   [SerializeField] private MouseLook m_MouseLook;
    22.   [SerializeField] private bool m_UseFovKick;
    23.   [SerializeField] private FOVKick m_FovKick = new FOVKick();
    24.   [SerializeField] private bool m_UseHeadBob;
    25.   [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
    26.   [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
    27.   [SerializeField] private float m_StepInterval;
    28.   [SerializeField] private AudioClip[] m_FootstepSounds;  // an array of footstep sounds that will be randomly selected from.
    29.   [SerializeField] private AudioClip m_JumpSound;  // the sound played when character leaves the ground.
    30.   [SerializeField] private AudioClip m_LandSound;  // the sound played when character touches back on ground.
    31.  
    32.   private Camera m_Camera;
    33.   private bool m_Jump;
    34.   private float m_YRotation;
    35.   private Vector2 m_Input;
    36.   private Vector3 m_MoveDir = Vector3.zero;
    37.   private CharacterController m_CharacterController;
    38.   private CollisionFlags m_CollisionFlags;
    39.   private bool m_PreviouslyGrounded;
    40.   private Vector3 m_OriginalCameraPosition;
    41.   private float m_StepCycle;
    42.   private float m_NextStep;
    43.   private bool m_Jumping;
    44.   private AudioSource m_AudioSource;
    45.  
    46.   // Use this for initialization
    47.   private void Start()
    48.   {
    49.   m_CharacterController = GetComponent<CharacterController>();
    50.   m_Camera = Camera.main;
    51.   m_OriginalCameraPosition = m_Camera.transform.localPosition;
    52.   m_FovKick.Setup(m_Camera);
    53.   m_HeadBob.Setup(m_Camera, m_StepInterval);
    54.   m_StepCycle = 0f;
    55.   m_NextStep = m_StepCycle/2f;
    56.   m_Jumping = false;
    57.   m_AudioSource = GetComponent<AudioSource>();
    58.        m_MouseLook.Init(transform , m_Camera.transform);
    59.   }
    60.  
    61.  
    62.   // Update is called once per frame
    63.   private void Update()
    64.   {
    65.   RotateView();
    66.   // the jump state needs to read here to make sure it is not missed
    67.   if (!m_Jump)
    68.   {
    69.   m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
    70.   }
    71.  
    72.   if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
    73.   {
    74.   StartCoroutine(m_JumpBob.DoBobCycle());
    75.   PlayLandingSound();
    76.   m_MoveDir.y = 0f;
    77.   m_Jumping = false;
    78.   }
    79.   if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
    80.   {
    81.   m_MoveDir.y = 0f;
    82.   }
    83.  
    84.   m_PreviouslyGrounded = m_CharacterController.isGrounded;
    85.   }
    86.  
    87.  
    88.   private void PlayLandingSound()
    89.   {
    90.   m_AudioSource.clip = m_LandSound;
    91.   m_AudioSource.Play();
    92.   m_NextStep = m_StepCycle + .5f;
    93.   }
    94.  
    95.  
    96.   private void FixedUpdate()
    97.   {
    98.   float speed;
    99.   GetInput(out speed);
    100.   // always move along the camera forward as it is the direction that it being aimed at
    101.   Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;
    102.  
    103.   // get a normal for the surface that is being touched to move along it
    104.   RaycastHit hitInfo;
    105.   Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
    106.   m_CharacterController.height/2f, ~0, QueryTriggerInteraction.Ignore);
    107.   desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
    108.  
    109.   m_MoveDir.x = desiredMove.x*speed;
    110.   m_MoveDir.z = desiredMove.z*speed;
    111.  
    112.  
    113.   if (m_CharacterController.isGrounded)
    114.   {
    115.   m_MoveDir.y = -m_StickToGroundForce;
    116.  
    117.   if (m_Jump)
    118.   {
    119.   m_MoveDir.y = m_JumpSpeed;
    120.   PlayJumpSound();
    121.   m_Jump = false;
    122.   m_Jumping = true;
    123.   }
    124.   }
    125.   else
    126.   {
    127.   m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
    128.   }
    129.   m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
    130.  
    131.   ProgressStepCycle(speed);
    132.   UpdateCameraPosition(speed);
    133.  
    134.   m_MouseLook.UpdateCursorLock();
    135.   }
    136.  
    137.  
    138.   private void PlayJumpSound()
    139.   {
    140.   m_AudioSource.clip = m_JumpSound;
    141.   m_AudioSource.Play();
    142.   }
    143.  
    144.  
    145.   private void ProgressStepCycle(float speed)
    146.   {
    147.   if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
    148.   {
    149.   m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
    150.   Time.fixedDeltaTime;
    151.   }
    152.  
    153.   if (!(m_StepCycle > m_NextStep))
    154.   {
    155.   return;
    156.   }
    157.  
    158.   m_NextStep = m_StepCycle + m_StepInterval;
    159.  
    160.   PlayFootStepAudio();
    161.   }
    162.  
    163.  
    164.   private void PlayFootStepAudio()
    165.   {
    166.   if (!m_CharacterController.isGrounded)
    167.   {
    168.   return;
    169.   }
    170.   // pick & play a random footstep sound from the array,
    171.   // excluding sound at index 0
    172.   int n = Random.Range(1, m_FootstepSounds.Length);
    173.   m_AudioSource.clip = m_FootstepSounds[n];
    174.   m_AudioSource.PlayOneShot(m_AudioSource.clip);
    175.   // move picked sound to index 0 so it's not picked next time
    176.   m_FootstepSounds[n] = m_FootstepSounds[0];
    177.   m_FootstepSounds[0] = m_AudioSource.clip;
    178.   }
    179.  
    180.  
    181.   private void UpdateCameraPosition(float speed)
    182.   {
    183.   Vector3 newCameraPosition;
    184.   if (!m_UseHeadBob)
    185.   {
    186.   return;
    187.   }
    188.   if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
    189.   {
    190.   m_Camera.transform.localPosition =
    191.   m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
    192.   (speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
    193.   newCameraPosition = m_Camera.transform.localPosition;
    194.   newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
    195.   }
    196.   else
    197.   {
    198.   newCameraPosition = m_Camera.transform.localPosition;
    199.   newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
    200.   }
    201.   m_Camera.transform.localPosition = newCameraPosition;
    202.   }
    203.  
    204.  
    205.   private void GetInput(out float speed)
    206.   {
    207.   // Read input
    208.   float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
    209.   float vertical = CrossPlatformInputManager.GetAxis("Vertical");
    210.  
    211.   bool waswalking = m_IsWalking;
    212.  
    213. #if !MOBILE_INPUT
    214.   // On standalone builds, walk/run speed is modified by a key press.
    215.   // keep track of whether or not the character is walking or running
    216.   m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
    217. #endif
    218.   // set the desired speed to be walking or running
    219.   speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
    220.   m_Input = new Vector2(horizontal, vertical);
    221.  
    222.   // normalize input if it exceeds 1 in combined length:
    223.   if (m_Input.sqrMagnitude > 1)
    224.   {
    225.   m_Input.Normalize();
    226.   }
    227.  
    228.   // handle speed change to give an fov kick
    229.   // only if the player is going to a run, is running and the fovkick is to be used
    230.   if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
    231.   {
    232.   StopAllCoroutines();
    233.   StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
    234.   }
    235.   }
    236.  
    237.  
    238.   private void RotateView()
    239.   {
    240.   m_MouseLook.LookRotation (transform, m_Camera.transform);
    241.   }
    242.  
    243.  
    244.   private void OnControllerColliderHit(ControllerColliderHit hit)
    245.   {
    246.   Rigidbody body = hit.collider.attachedRigidbody;
    247.   //dont move the rigidbody if the character is on top of it
    248.   if (m_CollisionFlags == CollisionFlags.Below)
    249.   {
    250.   return;
    251.   }
    252.  
    253.   if (body == null || body.isKinematic)
    254.   {
    255.   return;
    256.   }
    257.   body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
    258.   }
    259.   }
    260. }
    261.  
     
  30. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    Unity's standard FPSController tucks away MouseLook so it's not easily accessible. Create a C# script named ControlCursor and add it to the UI GameObject:

    ControlCursor.cs:
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class ControlCursor : MonoBehaviour
    4. {
    5.  
    6.     public void SetCursor(bool show)
    7.     {
    8.         Cursor.lockState = show ? CursorLockMode.None : CursorLockMode.Locked;
    9.         Cursor.visible = show;
    10.     }
    11. }
    Then hook up your Pause component like this:



    Also remove the Debug.Log("Time.timeScale: " + Time.timeScale) like from your Pause script. It'll kill your framerate.
     
  31. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Still does not work :( ! (Also don't forget that i am using another fps controller not the unity standard one.
     
  32. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    You're not using the script that you posted above? Please post the script you're actually using.
     
  33. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    that is the script i am using but it's not from the unity standard assets ;p !
     
  34. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    Whatever package included the script, it looks to be a copy of the Unity Standard Assets script. You can tell from this line in the script:

    namespace UnityStandardAssets.Characters.FirstPerson;

    Inspect your player GameObject in the scene. In the Inspector view, click on the value of the Script field. It'll be grayed out, but you can still click on it. This will highlight the script in the Project view. Is it the same as the script you posted above?

    Can you post a screenshot of your Pause component (like I did above) and a screenshot of the Inspector view of your player?
     
  35. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Capture.PNG _2.PNG
     
  36. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    When you open the pause menu, does the checkbox next to FirstPersonController become unticked?

    Try also add FPV_Example_Player and HotCursorManager to the OnPause() and OnUnPause() events. Configure their "bool enabled" properties the same way as FirstPersonController.
     
  37. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    The fpc script does not get unticked nope and as for the next step they don't have the enabled property and i guess that's because they are scripts and not objects like fpsc ?
     
  38. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    When you add a script to a GameObject, the instance of the script on the GameObject is called a component.

    Let's make sure we're talking about the same things. Although I may have been inconsistent in previous posts, from now on I'll try to make sure to use "script" to refer to the script file in the Project view and "component" to refer to the instance of the script that's been added to a GameObject.

    Don't assign the FirstPersonController script to the Pause component's OnPause() and OnUnPause() events. Assign the FPSController GameObject. Then use the dropdown to select the FirstPersonController component's enabled property. (This appears to be correct in your screenshot.)

    Then do the same for the FPV_ExamplePlayer component: Assign the FPSController GameObject, and select the FPV_ExamplePlayer component's enabled property. Repeat for HotCursorManager, too.

    Finally, double-check that your changes to the Pause script are still there. Specifically, the Pause() method should include this line:
    Code (csharp):
    1. onPause.Invoke();
    and the UnPause() method should include this line:
    Code (csharp):
    1. onUnPause.Invoke();
    It's that's all set up correctly, then when you play the scene and open the pause menu, the checkboxes next to the FPSController GameObject's FirstPersonController, FPVC_ExamplePlayer, and HotCursorManager components should all become unticked.

    If it doesn't work, check your scene in the Hierarchy view. Is there perhaps another player GameObject? Are the Pause component's OnPause() and OnUnPause() events perhaps pointing to the wrong GameObject?
     
  39. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Ok so, i did everything you said above and here is what i ended up with(hope this is correct) : Capture.PNG

    However it still does not work.Only the fps camera is able to move physics and other animations do stop when i press ESC but not the mouse.
     
  40. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    When you press ESC, inspect the FPSController GameObject. Does the checkbox next to the FirstPersonController component change from ticked to unticked? I know I sound like a broken record, but I just want to make sure that happens after the changes you made. That checkbox is the key. It has to change to unticked.

    I could take a look at your project if you'd like to send a copy to tony (at) pixelcrushers.com. If you do, zip up the Assets and ProjectSettings folders, and let me know which version of Unity to use.
     
  41. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Νope it doesn't get unticked it stays as checked!
     
  42. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    That's the problem. Let's see if we can figure out why.

    Edit your Pause script, and add some Debug.Log lines to the DoPause() method. Try something like this:

    Code (csharp):
    1. public void DoPause()
    2. {
    3.     //Set isPaused to true
    4.     isPaused = true;
    5.     //Set time.timescale to 0, this will cause animations and physics to stop updating
    6.     Time.timeScale = 0;
    7.     //call the ShowPausePanel function of the ShowPanels script
    8.     showPanels.ShowPausePanel ();
    9.     //my code
    10. // ADD THESE LINES BELOW:
    11.     var fpc = FindObjectOfType<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>();
    12.     var numFPC = FindObjectsOfType<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().Length;
    13.     Debug.Log("Before calling onPause.Invoke:");
    14.     Debug.Log("# of FirstPersonController components: " + numFPC);
    15.     Debug.Log("FirstPersonController.enabled: " + fpc.enabled, fpc);    
    16.     onPause.Invoke();
    17.     Debug.Log("After calling onPause.Invoke:");
    18.     Debug.Log("FirstPersonController.enabled: " + fpc.enabled, fpc);    
    19. }
    When you press ESC, you should see lines like this in the Console view:

    Before calling onPause.Invoke:
    # of FirstPersonController components: 1
    FirstPersonController.enabled: true
    After calling onPause.Invoke:
    FirstPersonController.enabled: false

    If it's different, let me know.

    Also, you can click on the "FirstPersonController.enabled:" lines to highlight which FirstPersonController component it's talking about.
     
  43. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    Ok i get 2 different sets of debug.log :

    1st set :
    Before calling onPause.Invoke:
    UnityEngine.Debug:Log(Object)
    Pause:DoPause() (at Assets/Game Jam Menu Template/Scripts/Pause.cs:61)
    Pause:Update() (at Assets/Game Jam Menu Template/Scripts/Pause.cs:30)

    2nd set :
    # of FirstPersonController components: 0
    UnityEngine.Debug:Log(Object)
    Pause:DoPause() (at Assets/Game Jam Menu Template/Scripts/Pause.cs:62)
    Pause:Update() (at Assets/Game Jam Menu Template/Scripts/Pause.cs:30)

    from what i see they are different from yours and the number of fpc is 0 instead of 1!
     
  44. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    That second line ("# of FirstPersonController components: 0") means Unity isn't finding any FirstPersonController components on any active GameObjects in the scene.

    Play your scene and click on your player in the Hierarchy view to inspect it. Make sure it's the actual player GameObject that you're controlling in the Game view and not, for example, some unused or inactive GameObject. Then check the Inspector. Find the FirstPersonController component, and click on the Script field to highlight the script that the component is based on. Is this the same script as the one you posted above?
     
  45. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    This can not be done since these two are in different scenes.The pause menu is attached to the UI gameobject in the main menu scene and the fps is in the main scene of the game.
     
  46. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    Okay, that's important to know. When you play the main menu scene and move into the main gameplay scene, the UI GameObject persists into the main gameplay scene, right? And your player GameObject is in the main gameplay scene?

    If so, create a script named "ControlPlayer" containing this code:

    ControlPlayer.cs:
    Code (csharp):
    1. using UnityEngine;
    2.    
    3. public class ControlPlayer : MonoBehaviour
    4. {
    5.    
    6.     public void SetPlayer(bool allowMovement)
    7.     {
    8.         var fpc = FindObjectOfType<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>();
    9.         if (fpc != null)
    10.         {
    11.             fpc.enabled = allowMovement;
    12.         }
    13.         else
    14.         {
    15.             Debug.LogWarning("Can't find a FirstPersonController in the current scene!");
    16.         }
    17.     }
    18. }
    Then add it to your UI GameObject.

    Remove all the items from the Pause component's OnPause() and OnUnPause() events except for the ControlCursor item. Then add another item slot to each event, assign the UI GameObject to the slot, and select ControlPlayer.SetPlayer. In the OnPause() event, leave the checkbox unticked. In the OnUnPause() event, tick the checkbox.

    If I understand correctly what's going on in your scene, that should hopefully do it.

    The one thing that concerns me is "# of FirstPersonController components: 0". If you're in the main gameplay scene, this should say "1".
     
  47. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    When i move from the main menu scene to the main scene the UI gameobject does not switch to the main scene,it disappears which seems logical right?So,since this does not happen should i add the code that you posted above r should we go with another solution?
     
  48. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    Don't use the code I posted just yet.

    The Pause component is on the UI GameObject, right?

    The Pause component is what opens the pause menu when you press ESC. How does it work in the main gameplay scene? Do you have another UI GameObject in the main gameplay scene?
     
  49. SP-Designs

    SP-Designs

    Joined:
    Oct 13, 2015
    Posts:
    184
    I don't have any other ui game objects in my main scene,it probably works through scripting i guess.Otherwise i don't get how this could work between two different scenes!
     
  50. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,521
    The Game Jam Menu Template's UI prefab has a component named Dont Destroy that should keep the UI GameObject in the scene when you switch to your main gameplay scene. However, when you click the Start button, the UI GameObject will hide its MenuPanel, which are the buttons for the start scene. So visually in the Game view and Scene view the UI will disappear, but the UI GameObject will still technically be in your scene.

    If you can confirm that this is the case in your project, then try that ControlPlayer.cs script I suggested above.