Search Unity

[Error] The name 'MouseLook' does not denote a valid type ('not found').

Discussion in 'Scripting' started by naruto-hokager, May 24, 2015.

  1. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Hello everyone !

    I followed tutorials to make a Pause Menu, but i get a error "The name 'MouseLook' does not denote a valid type ('not found').)
    I'm trying to solve it for like 4h omg... I even take a full script from another person which work in his video, but in Unity 3D 5, it give me the same error. I think Unity 5 change the API...

    Here is the full script :

    Code (JavaScript):
    1. var mainMenuSceneName : String;
    2. var pauseMenuFont : Font;
    3. private var pauseEnabled = false;
    4. private var PlayerCamera : MouseLook;
    5.  
    6. function Start(){
    7.     pauseEnabled = false;
    8.     Time.timeScale = 1;
    9.     AudioListener.volume = 1;
    10.     Cursor.visible = false;
    11. }
    12.  
    13. function Update(){
    14.  
    15.     //check if pause button (escape key) is pressed
    16.     if(Input.GetKeyDown("escape")){
    17.  
    18.         //check if game is already paused    
    19.         if(pauseEnabled == true){
    20.             //unpause the game
    21.             pauseEnabled = false;
    22.             Time.timeScale = 1;
    23.             AudioListener.volume = 1;
    24.             Cursor.visible = false;        
    25.         }
    26.      
    27.         //else if game isn't paused, then pause it
    28.         else if(pauseEnabled == false){
    29.             pauseEnabled = true;
    30.             AudioListener.volume = 0;
    31.             Time.timeScale = 0;
    32.             Cursor.visible = true;
    33.         }
    34.     }
    35. }
    36.  
    37. private var showGraphicsDropDown = false;
    38.  
    39. function OnGUI(){
    40.  
    41. GUI.skin.box.font = pauseMenuFont;
    42. GUI.skin.button.font = pauseMenuFont;
    43.  
    44.     if(pauseEnabled == true){
    45.      
    46.         //Make a background box
    47.         GUI.Box(Rect(Screen.width /2 - 100,Screen.height /2 - 100,250,200), "Pause Menu");
    48.      
    49.         //Make Main Menu button
    50.         if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 - 50,250,50), "Main Menu")){
    51.             Application.LoadLevel(mainMenuSceneName);
    52.         }
    53.      
    54.         //Make Change Graphics Quality button
    55.             if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 ,250,50), "Change Graphics Quality")){
    56.          
    57.             if(showGraphicsDropDown == false){
    58.                 showGraphicsDropDown = true;
    59.             }
    60.             else{
    61.                 showGraphicsDropDown = false;
    62.             }
    63.         }
    64.      
    65.         //Create the Graphics settings buttons, these won't show automatically, they will be called when
    66.         //the user clicks on the "Change Graphics Quality" Button, and then dissapear when they click
    67.         //on it again....
    68.         if(showGraphicsDropDown == true){
    69.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 ,250,50), "Fastest")){
    70.                 QualitySettings.currentLevel = QualityLevel.Fastest;
    71.             }
    72.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 50,250,50), "Fast")){
    73.                 QualitySettings.currentLevel = QualityLevel.Fast;
    74.             }
    75.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 100,250,50), "Simple")){
    76.                 QualitySettings.currentLevel = QualityLevel.Simple;
    77.             }
    78.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 150,250,50), "Good")){
    79.                 QualitySettings.currentLevel = QualityLevel.Good;
    80.             }
    81.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 200,250,50), "Beautiful")){
    82.                 QualitySettings.currentLevel = QualityLevel.Beautiful;
    83.             }
    84.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 250,250,50), "Fantastic")){
    85.                 QualitySettings.currentLevel = QualityLevel.Fantastic;
    86.             }
    87.          
    88.             if(Input.GetKeyDown("escape")){
    89.                 showGraphicsDropDown = false;
    90.             }
    91.         }
    92.      
    93.         //Make quit game button
    94.         if (GUI.Button (Rect (Screen.width /2 - 100,Screen.height /2 + 50,250,50), "Quit Game")){
    95.             Application.Quit();
    96.         }
    97.     }
    98. }

    Sorry for the long text, but i didn't find like a "code" option...

    Thank you in advance if you can help me ^^
     
    Last edited: May 24, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    really? there is a sticky about how to use code tags at the top of the forum....

    your problem is here:

    Code (csharp):
    1.  
    2. private var PlayerCamera : MouseLook;
    3.  
    you need to have a script called exactly "MouseLook" in your project for that to be a valid "type"
     
  3. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Oh yeah... Didn't see for the sticky... Thanks ! I edited the first post.

    Oh okay, i through "MouseLook" was a integrated command in the unity API that are link to the mouse look.
    The error actually disapear, but what is the command to stop the mouse looking ?

    EDIT: The script MouseLook just need to be in the project ? Nothing writted ?
     
  4. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Well, if you want it to actually do something, than yes, you'll want to write some code in the script (or use one that suits you from the Standard Assets, or elsewhere). You'll want this script to be attached to your "PlayerCamera" from the looks of things, not just stuck in a folder unassigned.
     
  5. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Okay, i'm gonna put a command in the MouseLook script. But do you know what is the correct command to stop the camera movement ? (In a FPS game i'm talking about)

    EDIT : Later on, i was so focused on this two lines but was not able to make them work :
    Code (JavaScript):
    1. firstPersonControllerCamera = gameObject.Find("First Person Controller").GetComponent("MouseLook");
    2. firstPersonControllerCamera.enabled = false;
     
  6. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Well, that code would simply look for the MouseLook script on your "First Person Controller" object, but not do anything with or to it... Then it would turn off whatever camera is tagged as MainCamera in your scene, which is probably not what you want to do... Rather than turning off the entire camera, you'd probably want to simply toggle off the "MouseLook" script so that it's not working anymore. Try changing line 2 to something like:
    Code (CSharp):
    1. firstPersonControllerCamera.enabled = false;
    Since you've already found the MouseLook script and assigned it to the firstPersonControllerCamera variable, this should disable that script until you re-enable it.
     
  7. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Eerr... Thanks for the explanation, wait i'm going to say what i think i understood :

    - Line 1 = Searching for "First Person Controller", then get the component "MouseLook" inside the Object "First Person Controller"
    - Line 2 = It disable the camera, so it will be black screen.

    If that is correct, then yea, it's not what i want. I want to toggle off like you said, the option MouseLook inside the script of First Person Controller.

    But the line you give me which i need to replace the line 2 is exactly the same code... ?

    If you want more explanation about what i want : I want to create a pause menu, and when we press escape, it lock the camera movement and show the pause menu. That's where i'm stuck.
     
  8. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Either I'm on drugs, or you altered the code you had posted... I see now that you have
    Code (CSharp):
    1.     firstPersonControllerCamera = gameObject.Find("First Person Controller").GetComponent("MouseLook");
    2.     firstPersonControllerCamera.enabled = false;
    But I'd have sworn it was
    Code (CSharp):
    1.     firstPersonControllerCamera = gameObject.Find("First Person Controller").GetComponent("MouseLook");
    2.     mainCamera.enabled = false;
    When I originally replied...

    Whatever the case, you should now be turning off the MouseLook script and not the entire camera...
     
  9. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Yeah sorry, the mainCamera.enabled=false; was a mistake so i edited the code, i'm going to test with :
    Code (JavaScript):
    1. firstPersonControllerCamera = gameObject.Find("First Person Controller").GetComponent("MouseLook");
    2.     firstPersonControllerCamera.enabled = false;
    Just a sec.
     
  10. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    I'll be here all week. Tip your waitress, try the veal, etc :p
     
  11. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    I can play but when i press escape it say :
    NullReferenceException: Object reference not set to an instance of an object
    PauseMenu.Update () (at Assets/Standard Assets/Characters/FirstPersonCharacter/Scripts/PauseMenu.js:34)

    By the way i change a little the script, just changed the var firstPersonControllerCamera to PlayerCamera and First Person Controller to PlayerCamera. Because i dont have firstPersonControllerCamera in my scene.

    Here the full code, just to be sure you got the correct version :

    Code (JavaScript):
    1. var mainMenuSceneName : String;
    2. var pauseMenuFont : Font;
    3. private var pauseEnabled = false;  
    4. private var PlayerCamera : MouseLook;  
    5.  
    6. function Start(){
    7.     pauseEnabled = false;
    8.     Time.timeScale = 1;
    9.     AudioListener.volume = 1;
    10.     Cursor.visible = false;
    11. }
    12.  
    13. function Update(){
    14.  
    15.     //check if pause button (escape key) is pressed
    16.     if(Input.GetKeyDown("escape")){
    17.    
    18.         //check if game is already paused      
    19.         if(pauseEnabled == true){
    20.             //unpause the game
    21.             pauseEnabled = false;
    22.             Time.timeScale = 1;
    23.             AudioListener.volume = 1;
    24.             Cursor.visible = false;          
    25.         }
    26.        
    27.         //else if game isn't paused, then pause it
    28.         else if(pauseEnabled == false){
    29.             pauseEnabled = true;
    30.             AudioListener.volume = 0;
    31.             Time.timeScale = 0;
    32.             Cursor.visible = true;
    33.             PlayerCamera = gameObject.Find("PlayerCamera").GetComponent("MouseLook");
    34.             PlayerCamera.enabled = false;
    35.  
    36.         }
    37.     }
    38. }
    39.  
    40. private var showGraphicsDropDown = false;
    41.  
    42. function OnGUI(){
    43.  
    44. GUI.skin.box.font = pauseMenuFont;
    45. GUI.skin.button.font = pauseMenuFont;
    46.  
    47.     if(pauseEnabled == true){
    48.        
    49.         //Make a background box
    50.         GUI.Box(Rect(Screen.width /2 - 100,Screen.height /2 - 100,250,200), "Pause Menu");
    51.        
    52.         //Make Main Menu button
    53.         if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 - 50,250,50), "Main Menu")){
    54.             Application.LoadLevel(mainMenuSceneName);
    55.         }
    56.        
    57.         //Make Change Graphics Quality button
    58.             if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 ,250,50), "Change Graphics Quality")){
    59.            
    60.             if(showGraphicsDropDown == false){
    61.                 showGraphicsDropDown = true;
    62.             }
    63.             else{
    64.                 showGraphicsDropDown = false;
    65.             }
    66.         }
    67.        
    68.         //Create the Graphics settings buttons, these won't show automatically, they will be called when
    69.         //the user clicks on the "Change Graphics Quality" Button, and then dissapear when they click
    70.         //on it again....
    71.         if(showGraphicsDropDown == true){
    72.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 ,250,50), "Fastest")){
    73.                 QualitySettings.currentLevel = QualityLevel.Fastest;
    74.             }
    75.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 50,250,50), "Fast")){
    76.                 QualitySettings.currentLevel = QualityLevel.Fast;
    77.             }
    78.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 100,250,50), "Simple")){
    79.                 QualitySettings.currentLevel = QualityLevel.Simple;
    80.             }
    81.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 150,250,50), "Good")){
    82.                 QualitySettings.currentLevel = QualityLevel.Good;
    83.             }
    84.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 200,250,50), "Beautiful")){
    85.                 QualitySettings.currentLevel = QualityLevel.Beautiful;
    86.             }
    87.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 250,250,50), "Fantastic")){
    88.                 QualitySettings.currentLevel = QualityLevel.Fantastic;
    89.             }
    90.            
    91.             if(Input.GetKeyDown("escape")){
    92.                 showGraphicsDropDown = false;
    93.             }
    94.         }
    95.        
    96.         //Make quit game button
    97.         if (GUI.Button (Rect (Screen.width /2 - 100,Screen.height /2 + 50,250,50), "Quit Game")){
    98.             Application.Quit();
    99.         }
    100.     }
    101. }
     
  12. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    OK, well the error you're receiving indicates that either (1) your camera isn't named PlayerCamera in your scene, or (2) there is no MouseLook script attached to it.
     
  13. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    It's the (2) who make problems, but when i try to add the script MouseLook to the PlayerCamera, i get this error : can't add script behaviour ! The scripts need to derive from MonoBehaviour.

    Maybe because there are nothing in the script except the default ? But i don't know what to add because i just want to stop the camera movements which the command are already in the script PauseMenu (It's the full code you can see up the page)

    Or i must add the code :
    Code (JavaScript):
    1. PlayerCamera = gameObject.Find("PlayerCamera").GetComponent("MouseLook");
    2.             PlayerCamera.enabled = false;
    to the script MouseLook ?

    Sorry for my english, i'm french...

    EDIT: I must go away for 1h, coming back ! Just saying so you don't wait xD
     
  14. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    My apologies, I again assumed you actually had some functionality in your MouseLook script... If you've just got a blank script there as a placeholder, it's not going to do you much good to turn it off and on since it doesn't do anything at all... Try adding this code to a new C# script named MouseLook, and then add that script to the camera you're trying to control:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. /// MouseLook rotates the transform based on the mouse delta.
    5. /// Minimum and Maximum values can be used to constrain the possible rotation
    6.  
    7. /// To make an FPS style character:
    8. /// - Create a capsule.
    9. /// - Add a rigid body to the capsule
    10. /// - Add the MouseLook script to the capsule.
    11. ///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
    12. /// - Add FPSWalker script to the capsule
    13.  
    14. /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
    15. /// - Add a MouseLook script to the camera.
    16. ///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
    17. [AddComponentMenu("Camera-Control/Mouse Look")]
    18. public class MouseLook : MonoBehaviour {
    19.    
    20.     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    21.     public RotationAxes axes = RotationAxes.MouseXAndY;
    22.     public float sensitivityX = 15F;
    23.     public float sensitivityY = 15F;
    24.    
    25.     public float minimumX = -360F;
    26.     public float maximumX = 360F;
    27.    
    28.     public float minimumY = -60F;
    29.     public float maximumY = 60F;
    30.    
    31.     float rotationX = 0F;
    32.     float rotationY = 0F;
    33.    
    34.     Quaternion originalRotation;
    35.    
    36.     void Update ()
    37.     {
    38.         if (axes == RotationAxes.MouseXAndY)
    39.         {
    40.             // Read the mouse input axis
    41.             rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    42.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    43.            
    44.             rotationX = ClampAngle (rotationX, minimumX, maximumX);
    45.             rotationY = ClampAngle (rotationY, minimumY, maximumY);
    46.            
    47.             Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
    48.             Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
    49.            
    50.             transform.localRotation = originalRotation * xQuaternion * yQuaternion;
    51.         }
    52.         else if (axes == RotationAxes.MouseX)
    53.         {
    54.             rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    55.             rotationX = ClampAngle (rotationX, minimumX, maximumX);
    56.            
    57.             Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
    58.             transform.localRotation = originalRotation * xQuaternion;
    59.         }
    60.         else
    61.         {
    62.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    63.             rotationY = ClampAngle (rotationY, minimumY, maximumY);
    64.            
    65.             Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
    66.             transform.localRotation = originalRotation * yQuaternion;
    67.         }
    68.     }
    69.    
    70.     void Start ()
    71.     {
    72.         // Make the rigid body not change rotation
    73.         if (GetComponent<Rigidbody>())
    74.             GetComponent<Rigidbody>().freezeRotation = true;
    75.         originalRotation = transform.localRotation;
    76.     }
    77.    
    78.     public static float ClampAngle (float angle, float min, float max)
    79.     {
    80.         if (angle < -360F)
    81.             angle += 360F;
    82.         if (angle > 360F)
    83.             angle -= 360F;
    84.         return Mathf.Clamp (angle, min, max);
    85.     }
    86. }
     
  15. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    In fact i got MouseLook from the Standard Asset of Unity, and i got the MouseLook1 which i created who are empty.

    I added your script code to MouseLook1, but when i'm trying to put it to the camera, i got this error :
     
  16. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Well, as the error states, the class and filename must match. If you placed the code I posted into MouseLook1, you'd need to change the line near the top from
    Code (CSharp):
    1. public class MouseLook : MonoBehaviour {
    to
    Code (CSharp):
    1. public class MouseLook1 : MonoBehaviour {
     
  17. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Oh yeah... I'm stupid >.<
    It work, i success to add the script MouseLook1 to the camera.

    I tried running the game, press escape, the menu come but the camera still moving.


    So, i put MouseLook1 on the PlayerCamera, the PauseMenu too. And the FirstPersonController are on Player (Top of Hierarchy).
    PauseMenu script are the same as i showed you.

    Aaaaand... Camera still moving when the pause menu come...

    God damned, i know why i prefer level design than scripting xD

    EDIT: Need to go sleep ! We'll continue this battle tomorrow xD Good night !
     
  18. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Did you also change
    Code (CSharp):
    1. PlayerCamera = gameObject.Find("PlayerCamera").GetComponent("MouseLook");
    2. PlayerCamera.enabled = false;
    3.  
    to
    Code (CSharp):
    1. PlayerCamera = gameObject.Find("PlayerCamera").GetComponent("MouseLook1");
    2. PlayerCamera.enabled = false;
    3.  
    ?
     
  19. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Yes, do i need to change the variable to ?
    Code (JavaScript):
    1. private var PlayerCamera : MouseLook;
    to
    Code (JavaScript):
    1. private var PlayerCamera : MouseLook1;
    ?

    When i tried to change it, another error...
    Code (JavaScript):
    1. The name 'MouseLook1' does not denote a valid type ('not found')
     
  20. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Err... My apologies, I gave you C# when you're using Javascript... They don't always play well together. Here's someone else's JS translation of the MouseLook code, try it instead of the C# script I gave you.

    Code (JavaScript):
    1. public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 };
    2. public var axes = RotationAxes.MouseXAndY;
    3. public var sensitivityX : float = 15F;
    4. public var sensitivityY : float = 15F;
    5.  
    6. public var minimumX : float = -360F;
    7. public var maximumX : float = 360F;
    8.  
    9. public var minimumY : float = -60F;
    10. public var maximumY : float = 60F;
    11.  
    12. var rotationY : float = 0F;
    13.  
    14. function Start ()
    15. {
    16. // Make the rigid body not change rotation
    17. if (GetComponent.<Rigidbody>())
    18.      GetComponent.<Rigidbody>().freezeRotation = true;
    19. }
    20.  
    21. function Update ()
    22. {
    23. if (axes == RotationAxes.MouseXAndY)
    24. {
    25.      var rotationX : float = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
    26.    
    27.      rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    28.      rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    29.    
    30.      transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    31. }
    32. else if (axes == RotationAxes.MouseX)
    33. {
    34.      transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    35. }
    36. else
    37. {
    38.      rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    39.      rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    40.    
    41.      transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
    42. }
    43. }
    Name it MouseLook or MouseLook1 (depending on what your current code is looking for) and give that a shot.
     
  21. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    No problem ! Thanks for still helping me :)

    So i have MouseLook1 in Javascript, PauseMenu in Javascript too.
    I put your new code in MouseLook1 and PauseMenu and MouseLook1 are on PlayerCamera.
    I can play and press escape without any error ! :D But the camera still moving in the pause menu... >.<

    Maybe "PlayerCamera.enabled = false;" is not the good command to stop the camera movement ?

    EDIT : Ooooooooh wait i think i know, a sec
     
  22. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    I can see that the MouseLook1 is disabled when i press escape, but the principal script who give me camera control is not MouseLook1, it's the FirstPersonController which are in Player object.
    This script give me all the control but the MouseLook are integrated to the script, like a all-in-one.

    I'm going to try to fix all this
     
  23. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Yeah i need to disable FirstPersonController , but when i'm trying :
    Code (JavaScript):
    1. The name 'FirstPersonController' does not denote a valid type ('not found').
    2.  
    Probably because PauseMenu is in Javascript and FirstPersonController in C#...

    You got a idea ? :c

    Maybe i can delete the part MouseLook of the FirstPersonController controller....
     
    Last edited: May 25, 2015
  24. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    No worries, think I've got it worked out for you... Replace your pause and mouse-look scripts with the two below and see how you fare:

    Pause:
    Code (JavaScript):
    1.     var mainMenuSceneName : String;
    2.     var pauseMenuFont : Font;
    3.     private var pauseEnabled = false;
    4.     private var PlayerCamera : MouseLook;
    5.    
    6.     function Start(){
    7.         pauseEnabled = false;
    8.         Time.timeScale = 1;
    9.         AudioListener.volume = 1;
    10.         Cursor.visible = false;
    11.         PlayerCamera = gameObject.Find("PlayerCamera").GetComponent("MouseLook");
    12.     }
    13.    
    14.     function Update(){
    15.    
    16.         //check if pause button (escape key) is pressed
    17.         if(Input.GetKeyDown("escape")){
    18.      
    19.             //check if game is already paused    
    20.             if(pauseEnabled == true){
    21.                 //unpause the game
    22.                 pauseEnabled = false;
    23.                 Time.timeScale = 1;
    24.                 AudioListener.volume = 1;
    25.                 Cursor.visible = false;        
    26.                 PlayerCamera.DontMove(false);
    27.             }
    28.          
    29.             //else if game isn't paused, then pause it
    30.             else if(pauseEnabled == false){
    31.                 pauseEnabled = true;
    32.                 AudioListener.volume = 0;
    33.                 Time.timeScale = 0;
    34.                 Cursor.visible = true;
    35.                 PlayerCamera.DontMove(true);
    36.    
    37.             }
    38.         }
    39.     }
    40.    
    41.     private var showGraphicsDropDown = false;
    42.    
    43.     function OnGUI(){
    44.    
    45.     GUI.skin.box.font = pauseMenuFont;
    46.     GUI.skin.button.font = pauseMenuFont;
    47.    
    48.         if(pauseEnabled == true){
    49.          
    50.             //Make a background box
    51.             GUI.Box(Rect(Screen.width /2 - 100,Screen.height /2 - 100,250,200), "Pause Menu");
    52.          
    53.             //Make Main Menu button
    54.             if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 - 50,250,50), "Main Menu")){
    55.                 Application.LoadLevel(mainMenuSceneName);
    56.             }
    57.          
    58.             //Make Change Graphics Quality button
    59.                 if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 ,250,50), "Change Graphics Quality")){
    60.              
    61.                 if(showGraphicsDropDown == false){
    62.                     showGraphicsDropDown = true;
    63.                 }
    64.                 else{
    65.                     showGraphicsDropDown = false;
    66.                 }
    67.             }
    68.          
    69.             //Create the Graphics settings buttons, these won't show automatically, they will be called when
    70.             //the user clicks on the "Change Graphics Quality" Button, and then dissapear when they click
    71.             //on it again....
    72.             if(showGraphicsDropDown == true){
    73.                 if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 ,250,50), "Fastest")){
    74.                     QualitySettings.currentLevel = QualityLevel.Fastest;
    75.                 }
    76.                 if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 50,250,50), "Fast")){
    77.                     QualitySettings.currentLevel = QualityLevel.Fast;
    78.                 }
    79.                 if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 100,250,50), "Simple")){
    80.                     QualitySettings.currentLevel = QualityLevel.Simple;
    81.                 }
    82.                 if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 150,250,50), "Good")){
    83.                     QualitySettings.currentLevel = QualityLevel.Good;
    84.                 }
    85.                 if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 200,250,50), "Beautiful")){
    86.                     QualitySettings.currentLevel = QualityLevel.Beautiful;
    87.                 }
    88.                 if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 250,250,50), "Fantastic")){
    89.                     QualitySettings.currentLevel = QualityLevel.Fantastic;
    90.                 }
    91.              
    92.                 if(Input.GetKeyDown("escape")){
    93.                     showGraphicsDropDown = false;
    94.                 }
    95.             }
    96.          
    97.             //Make quit game button
    98.             if (GUI.Button (Rect (Screen.width /2 - 100,Screen.height /2 + 50,250,50), "Quit Game")){
    99.                 Application.Quit();
    100.             }
    101.         }
    102.     }
    MouseLook:
    Code (JavaScript):
    1. public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 };
    2. public var axes = RotationAxes.MouseXAndY;
    3. public var sensitivityX : float = 15F;
    4. public var sensitivityY : float = 15F;
    5.  
    6. public var minimumX : float = -360F;
    7. public var maximumX : float = 360F;
    8.  
    9. public var minimumY : float = -60F;
    10. public var maximumY : float = 60F;
    11.  
    12. private var dontMove : boolean = false;
    13.  
    14. var rotationY : float = 0F;
    15.  
    16. function Start ()
    17. {
    18. // Make the rigid body not change rotation
    19. if (GetComponent.<Rigidbody>())
    20.      GetComponent.<Rigidbody>().freezeRotation = true;
    21. }
    22.  
    23. function Update ()
    24. {
    25. if(dontMove == true)
    26. {
    27.      return;
    28. }
    29. if (axes == RotationAxes.MouseXAndY)
    30. {
    31.      var rotationX : float = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
    32.    
    33.      rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    34.      rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    35.    
    36.      transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    37. }
    38. else if (axes == RotationAxes.MouseX)
    39. {
    40.      transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    41. }
    42. else
    43. {
    44.      rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    45.      rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    46.    
    47.      transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
    48. }
    49. }
    50.  
    51. function DontMove(e : boolean)
    52. {
    53.     if (e == true)
    54.     {
    55.         dontMove = true;
    56.     }
    57.     if(e == false)
    58.     {
    59.         dontMove = false;
    60.     }
    61. }
     
  25. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Not really working... :( But in fact, with the FirstPersonController, there are 2 MouseLook, that's why camera still moving in Pause Menu.

    EDIT :
    The previous script look better because it disable correctly the MouseLook1.

    The problem is that we need to deactivate or remove somehow the MouseLook part of the script FirstPersonController
     
  26. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    OK, look... You've got way too many scripts all trying to do the same thing from the sounds of it... If your FirstPersonController script handles the MouseLook behavior, then we don't need to be adding more scripts that do the same thing. It's just confusing and confounding the issue... Yank the extra MouseLook scripts off of there and show me the code for your FirstPersonController. I'll hack the code we need in from there.

    Sorry, it's been a looooong night....
     
  27. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Sorry to get you in this trouble...
    I removed MouseLook1

    Here the full code of FirstPersonController in C# from the Standard Asset

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

    Code (JavaScript):
    1. var mainMenuSceneName : String;
    2. var pauseMenuFont : Font;
    3. private var pauseEnabled = false;  
    4. private var PlayerCamera : MouseLook;  
    5.  
    6. function Start(){
    7.     pauseEnabled = false;
    8.     Time.timeScale = 1;
    9.     AudioListener.volume = 1;
    10.     Cursor.visible = false;
    11. }
    12.  
    13. function Update(){
    14.  
    15.     //check if pause button (escape key) is pressed
    16.     if(Input.GetKeyDown("escape")){
    17.    
    18.         //check if game is already paused      
    19.         if(pauseEnabled == true){
    20.             //unpause the game
    21.             pauseEnabled = false;
    22.             Time.timeScale = 1;
    23.             AudioListener.volume = 1;
    24.             Cursor.visible = false;          
    25.         }
    26.        
    27.         //else if game isn't paused, then pause it
    28.         else if(pauseEnabled == false){
    29.             pauseEnabled = true;
    30.             AudioListener.volume = 0;
    31.             Time.timeScale = 0;
    32.             Cursor.visible = true;
    33.             PlayerCamera = gameObject.Find("PlayerCamera").GetComponent("MouseLook");
    34.             PlayerCamera.enabled = false;
    35.  
    36.         }
    37.     }
    38. }
    39.  
    40. private var showGraphicsDropDown = false;
    41.  
    42. function OnGUI(){
    43.  
    44. GUI.skin.box.font = pauseMenuFont;
    45. GUI.skin.button.font = pauseMenuFont;
    46.  
    47.     if(pauseEnabled == true){
    48.        
    49.         //Make a background box
    50.         GUI.Box(Rect(Screen.width /2 - 100,Screen.height /2 - 100,250,200), "Pause Menu");
    51.        
    52.         //Make Main Menu button
    53.         if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 - 50,250,50), "Main Menu")){
    54.             Application.LoadLevel(mainMenuSceneName);
    55.         }
    56.        
    57.         //Make Change Graphics Quality button
    58.             if(GUI.Button(Rect(Screen.width /2 - 100,Screen.height /2 ,250,50), "Change Graphics Quality")){
    59.            
    60.             if(showGraphicsDropDown == false){
    61.                 showGraphicsDropDown = true;
    62.             }
    63.             else{
    64.                 showGraphicsDropDown = false;
    65.             }
    66.         }
    67.        
    68.         //Create the Graphics settings buttons, these won't show automatically, they will be called when
    69.         //the user clicks on the "Change Graphics Quality" Button, and then dissapear when they click
    70.         //on it again....
    71.         if(showGraphicsDropDown == true){
    72.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 ,250,50), "Fastest")){
    73.                 QualitySettings.currentLevel = QualityLevel.Fastest;
    74.             }
    75.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 50,250,50), "Fast")){
    76.                 QualitySettings.currentLevel = QualityLevel.Fast;
    77.             }
    78.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 100,250,50), "Simple")){
    79.                 QualitySettings.currentLevel = QualityLevel.Simple;
    80.             }
    81.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 150,250,50), "Good")){
    82.                 QualitySettings.currentLevel = QualityLevel.Good;
    83.             }
    84.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 200,250,50), "Beautiful")){
    85.                 QualitySettings.currentLevel = QualityLevel.Beautiful;
    86.             }
    87.             if(GUI.Button(Rect(Screen.width /2 + 150,Screen.height /2 + 250,250,50), "Fantastic")){
    88.                 QualitySettings.currentLevel = QualityLevel.Fantastic;
    89.             }
    90.            
    91.             if(Input.GetKeyDown("escape")){
    92.                 showGraphicsDropDown = false;
    93.             }
    94.         }
    95.        
    96.         //Make quit game button
    97.         if (GUI.Button (Rect (Screen.width /2 - 100,Screen.height /2 + 50,250,50), "Quit Game")){
    98.             Application.Quit();
    99.         }
    100.     }
    101. }
     
  28. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    OK, first things first, try not to mix C# and JS... Pick one and stick with it... I'll see if I can translate your Pause script into C# for you so that they'll communicate with each other and not cause further problems... Give me a bit, I'm juggling a lot at the moment.
     
  29. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Well i follow tutorials and they use JS, that's why i choose it but Standard Asset have C# :( But okay i'll remember that.

    Take your time, thank you very much
     
  30. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    It's quite alright, most of us started out with Javascript since it's a bit simpler to wrap your head around initially. I highly recommend learning C# though.

    Ugh... I'm batting 1000 tonight (not!)... For whatever reason, I can't get the FPS Controller to communicate with the PauseMenu script, even after converting it to C#... This is the second major headache in a row over something that seemed awfully simple. Going to get some coffee in me and attack it again in a minute or five. Sorry for the delay!
     
    Last edited: May 25, 2015
  31. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    The FPS script itself is stuck in a namespace that makes it inaccessible to other scripts... It's beyond me (at the moment at least) how to get them talking to each other and the few ideas I've had have all fallen short. I'm going to have to walk away and hope that someone with a bit more knowledge can assist you. If and when they do, I'll certainly be back to see the solution for myself. Who knows, maybe I'll wake up screaming "Eureka!" later today and have a solution. For the moment, however, I'm at a loss. Sorry to let you down.
     
    Last edited: May 26, 2015
  32. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    I'm pretty impress that Unity even beat you ! I think i'm going to try to make a player movement, bob head and all that in C#, then i will be able to disable whatever i want.

    Thank you very much for your help Krougeau !! Even if you didn't success, i really appreciate your help :)
     
    krougeau likes this.
  33. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    You're most certainly welcome. I'm still learning myself, but I thought I had a pretty good handle on cross-script communication until now, haha. Ah well, hopefully someone can set us both straight. Have a good one & best of luck!
     
  34. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Thank you ! Yeah i hope someone can help us too xD

    Just to know what your preference, do you prefer C# or Javascript ? Just to know which i need to learn. I heard that Javascript is unique to Unity3D and are more easy, C# is for everything but more complexe, and other opinion so i'm a little lost...
     
  35. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Javascript is a bit more "forgiving" in the kinds of variables you can declare, but this can lead to problems, too. Because you don't have to specifically declare what types of variables your variables are, you can run into problems, especially when debugging. For instance, you can get an error that says "Whatever" is invalid, but it doesn't tell you if that's because it's an int when the code was expecting a float, or a string when it wanted a Vector3... things like that. C# is a bit less forgiving and requires you to specifically state what kinds of variables you're using (except in locally within functions where you can get away with using plain old "var" declarations -- still, once you get used to declaring them, you'll likely not use var much).

    In short, I'd suggest jumping on the C# bandwagon as soon as you can :p It's really not any more difficult than JS, it's just formatted and phrased a bit differently. Once you wrap your head around those basic differences, it's easy peasy.
     
  36. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Oooooh okay, if i understand correctly :

    - Javascript is like "Declare or not the type of the variable" but after, JS think we are talking about float when we wanted to talk about int.
    - C# forces us to declare the type of the variable so he understand what we want exaclty and so that make no problem of misunderstood.

    In that way, for sure C# is better !! :D
     
    krougeau likes this.
  37. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Yep, that's the gist of it :) Also, the more you look around, the more you'll find that C# is used a lot more widely than simple Javascript / Unityscript, so you'll get a lot more mileage out of it once you get the hang of it.
     
  38. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Okay :eek: I'm going to focus on C# mainly then :D

    Thanks for the explanation !! :)
     
    krougeau likes this.
  39. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Back and semi-conscious, haha ;) Going to take a fresh look at things and see if I can getcha running.
     
  40. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    By Jove, I think we've got it! :p

    Here's an altered version of the FirstPersonController script:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityStandardAssets.CrossPlatformInput;
    4. using UnityStandardAssets.Utility;
    5. using Random = UnityEngine.Random;
    6.  
    7. namespace UnityStandardAssets.Characters.FirstPerson
    8. {
    9.     [RequireComponent(typeof (CharacterController))]
    10.     [RequireComponent(typeof (AudioSource))]
    11.     public class FirstPersonController : MonoBehaviour
    12.     {
    13.         [SerializeField] private bool m_IsWalking;
    14.         [SerializeField] private float m_WalkSpeed;
    15.         [SerializeField] private float m_RunSpeed;
    16.         [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
    17.         [SerializeField] private float m_JumpSpeed;
    18.         [SerializeField] private float m_StickToGroundForce;
    19.         [SerializeField] private float m_GravityMultiplier;
    20.         [SerializeField] private MouseLook m_MouseLook;
    21.         [SerializeField] private bool m_UseFovKick;
    22.         [SerializeField] private FOVKick m_FovKick = new FOVKick();
    23.         [SerializeField] private bool m_UseHeadBob;
    24.         [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
    25.         [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
    26.         [SerializeField] private float m_StepInterval;
    27.         [SerializeField] private AudioClip[] m_FootstepSounds;    // an array of footstep sounds that will be randomly selected from.
    28.         [SerializeField] private AudioClip m_JumpSound;           // the sound played when character leaves the ground.
    29.         [SerializeField] private AudioClip m_LandSound;           // the sound played when character touches back on ground.
    30.  
    31.         private Camera m_Camera;
    32.         private bool m_Jump;
    33.         private float m_YRotation;
    34.         private Vector2 m_Input;
    35.         private Vector3 m_MoveDir = Vector3.zero;
    36.         private CharacterController m_CharacterController;
    37.         private CollisionFlags m_CollisionFlags;
    38.         private bool m_PreviouslyGrounded;
    39.         private Vector3 m_OriginalCameraPosition;
    40.         private float m_StepCycle;
    41.         private float m_NextStep;
    42.         private bool m_Jumping;
    43.         private AudioSource m_AudioSource;
    44.         private bool canMove = true;
    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.             if(canMove == false)
    66.             {
    67.                 return;
    68.             }
    69.             RotateView();
    70.             // the jump state needs to read here to make sure it is not missed
    71.             if (!m_Jump)
    72.             {
    73.                 m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
    74.             }
    75.  
    76.             if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
    77.             {
    78.                 StartCoroutine(m_JumpBob.DoBobCycle());
    79.                 PlayLandingSound();
    80.                 m_MoveDir.y = 0f;
    81.                 m_Jumping = false;
    82.             }
    83.             if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
    84.             {
    85.                 m_MoveDir.y = 0f;
    86.             }
    87.  
    88.             m_PreviouslyGrounded = m_CharacterController.isGrounded;
    89.         }
    90.  
    91.  
    92.         private void PlayLandingSound()
    93.         {
    94.             m_AudioSource.clip = m_LandSound;
    95.             m_AudioSource.Play();
    96.             m_NextStep = m_StepCycle + .5f;
    97.         }
    98.  
    99.  
    100.         private void FixedUpdate()
    101.         {
    102.             if(canMove == false)
    103.             {
    104.                 return;
    105.             }
    106.             float speed;
    107.             GetInput(out speed);
    108.             // always move along the camera forward as it is the direction that it being aimed at
    109.             Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;
    110.  
    111.             // get a normal for the surface that is being touched to move along it
    112.             RaycastHit hitInfo;
    113.             Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
    114.                                m_CharacterController.height/2f);
    115.             desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
    116.  
    117.             m_MoveDir.x = desiredMove.x*speed;
    118.             m_MoveDir.z = desiredMove.z*speed;
    119.  
    120.  
    121.             if (m_CharacterController.isGrounded)
    122.             {
    123.                 m_MoveDir.y = -m_StickToGroundForce;
    124.  
    125.                 if (m_Jump)
    126.                 {
    127.                     m_MoveDir.y = m_JumpSpeed;
    128.                     PlayJumpSound();
    129.                     m_Jump = false;
    130.                     m_Jumping = true;
    131.                 }
    132.             }
    133.             else
    134.             {
    135.                 m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
    136.             }
    137.             m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
    138.  
    139.             ProgressStepCycle(speed);
    140.             UpdateCameraPosition(speed);
    141.         }
    142.  
    143.         public void MouseToggle()
    144.         {
    145.             if(canMove == true)
    146.             {
    147.                 canMove = false;
    148.                 return;
    149.             }
    150.             if(canMove == false)
    151.             {
    152.                 canMove = true;
    153.                 return;
    154.             }
    155.         }
    156.  
    157.         private void PlayJumpSound()
    158.         {
    159.             m_AudioSource.clip = m_JumpSound;
    160.             m_AudioSource.Play();
    161.         }
    162.  
    163.  
    164.         private void ProgressStepCycle(float speed)
    165.         {
    166.             if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
    167.             {
    168.                 m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
    169.                              Time.fixedDeltaTime;
    170.             }
    171.  
    172.             if (!(m_StepCycle > m_NextStep))
    173.             {
    174.                 return;
    175.             }
    176.  
    177.             m_NextStep = m_StepCycle + m_StepInterval;
    178.  
    179.             PlayFootStepAudio();
    180.         }
    181.  
    182.  
    183.         private void PlayFootStepAudio()
    184.         {
    185.             if (!m_CharacterController.isGrounded)
    186.             {
    187.                 return;
    188.             }
    189.             // pick & play a random footstep sound from the array,
    190.             // excluding sound at index 0
    191.             int n = Random.Range(1, m_FootstepSounds.Length);
    192.             m_AudioSource.clip = m_FootstepSounds[n];
    193.             m_AudioSource.PlayOneShot(m_AudioSource.clip);
    194.             // move picked sound to index 0 so it's not picked next time
    195.             m_FootstepSounds[n] = m_FootstepSounds[0];
    196.             m_FootstepSounds[0] = m_AudioSource.clip;
    197.         }
    198.  
    199.  
    200.         private void UpdateCameraPosition(float speed)
    201.         {
    202.             Vector3 newCameraPosition;
    203.             if (!m_UseHeadBob)
    204.             {
    205.                 return;
    206.             }
    207.             if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
    208.             {
    209.                 m_Camera.transform.localPosition =
    210.                     m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
    211.                                       (speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
    212.                 newCameraPosition = m_Camera.transform.localPosition;
    213.                 newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
    214.             }
    215.             else
    216.             {
    217.                 newCameraPosition = m_Camera.transform.localPosition;
    218.                 newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
    219.             }
    220.             m_Camera.transform.localPosition = newCameraPosition;
    221.         }
    222.  
    223.  
    224.         private void GetInput(out float speed)
    225.         {
    226.             // Read input
    227.             float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
    228.             float vertical = CrossPlatformInputManager.GetAxis("Vertical");
    229.  
    230.             bool waswalking = m_IsWalking;
    231.  
    232. #if !MOBILE_INPUT
    233.             // On standalone builds, walk/run speed is modified by a key press.
    234.             // keep track of whether or not the character is walking or running
    235.             m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
    236. #endif
    237.             // set the desired speed to be walking or running
    238.             speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
    239.             m_Input = new Vector2(horizontal, vertical);
    240.  
    241.             // normalize input if it exceeds 1 in combined length:
    242.             if (m_Input.sqrMagnitude > 1)
    243.             {
    244.                 m_Input.Normalize();
    245.             }
    246.  
    247.             // handle speed change to give an fov kick
    248.             // only if the player is going to a run, is running and the fovkick is to be used
    249.             if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
    250.             {
    251.                 StopAllCoroutines();
    252.                 StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
    253.             }
    254.         }
    255.  
    256.  
    257.         private void RotateView()
    258.         {
    259.             m_MouseLook.LookRotation (transform, m_Camera.transform);
    260.         }
    261.  
    262.  
    263.         private void OnControllerColliderHit(ControllerColliderHit hit)
    264.         {
    265.             Rigidbody body = hit.collider.attachedRigidbody;
    266.             //dont move the rigidbody if the character is on top of it
    267.             if (m_CollisionFlags == CollisionFlags.Below)
    268.             {
    269.                 return;
    270.             }
    271.  
    272.             if (body == null || body.isKinematic)
    273.             {
    274.                 return;
    275.             }
    276.             body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
    277.         }
    278.     }
    279. }
    And here's an updated, C# version of the PauseMenu script with proper references (and likely some superfluous ones too, but I wanted to be sure/safe haha). Make sure you create a new C# script to paste the following code into and name it PauseMenu or you'll get errors:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityStandardAssets.Utility;
    4. using UnityStandardAssets.CrossPlatformInput;
    5. using UnityStandardAssets.Characters.FirstPerson;
    6.  
    7. public class PauseMenu : MonoBehaviour
    8. {
    9.     public string mainMenuSceneName;
    10.     public Font pauseMenuFont;
    11.     private bool pauseEnabled = false;
    12.     private bool showGraphicsDropDown = false;
    13.     private GameObject fps;
    14.     // private MouseLook mlook;
    15.  
    16.     void Start()
    17.     {
    18.         pauseEnabled = false;
    19.         Time.timeScale = 1;
    20.         AudioListener.volume = 1;
    21.         Cursor.visible = false;
    22.         fps = GameObject.Find("FPSController");
    23.         //mlook = GameObject.Find("FPSController").GetComponent<MouseLook>();
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.    
    29.         //check if pause button (escape key) is pressed
    30.         if(Input.GetKeyDown("escape")){
    31.        
    32.             //check if game is already paused  
    33.             if(pauseEnabled == true){
    34.                 //unpause the game
    35.                 pauseEnabled = false;
    36.                 Time.timeScale = 1;
    37.                 AudioListener.volume = 1;
    38.                 Cursor.visible = false;
    39.                 fps.BroadcastMessage("MouseToggle");
    40.             }
    41.        
    42.             //else if game isn't paused, then pause it
    43.             else if(pauseEnabled == false){
    44.                 pauseEnabled = true;
    45.                 AudioListener.volume = 0;
    46.                 Time.timeScale = 0;
    47.                 Cursor.visible = true;
    48.                 fps.BroadcastMessage("MouseToggle");
    49.             }
    50.         }
    51.     }
    52.  
    53.     void OnGUI()
    54.     {
    55.    
    56.         GUI.skin.box.font = pauseMenuFont;
    57.         GUI.skin.button.font = pauseMenuFont;
    58.    
    59.         if(pauseEnabled == true){
    60.        
    61.             //Make a background box
    62.             GUI.Box(new Rect(Screen.width /2 - 100,Screen.height /2 - 100,250,200), "Pause Menu");
    63.        
    64.             //Make Main Menu button
    65.             if(GUI.Button(new Rect(Screen.width /2 - 100,Screen.height /2 - 50,250,50), "Main Menu")){
    66.                 Application.LoadLevel(mainMenuSceneName);
    67.             }
    68.        
    69.             //Make Change Graphics Quality button
    70.             if(GUI.Button(new Rect(Screen.width /2 - 100,Screen.height /2 ,250,50), "Change Graphics Quality")){
    71.            
    72.                 if(showGraphicsDropDown == false){
    73.                     showGraphicsDropDown = true;
    74.                 }
    75.                 else{
    76.                     showGraphicsDropDown = false;
    77.                 }
    78.             }
    79.        
    80.             //Create the Graphics settings buttons, these won't show automatically, they will be called when
    81.             //the user clicks on the "Change Graphics Quality" Button, and then dissapear when they click
    82.             //on it again....
    83.             if(showGraphicsDropDown == true){
    84.                 if(GUI.Button(new Rect(Screen.width /2 + 150,Screen.height /2 ,250,50), "Fastest")){
    85.                     QualitySettings.currentLevel = QualityLevel.Fastest;
    86.                 }
    87.                 if(GUI.Button(new Rect(Screen.width /2 + 150,Screen.height /2 + 50,250,50), "Fast")){
    88.                     QualitySettings.currentLevel = QualityLevel.Fast;
    89.                 }
    90.                 if(GUI.Button(new Rect(Screen.width /2 + 150,Screen.height /2 + 100,250,50), "Simple")){
    91.                     QualitySettings.currentLevel = QualityLevel.Simple;
    92.                 }
    93.                 if(GUI.Button(new Rect(Screen.width /2 + 150,Screen.height /2 + 150,250,50), "Good")){
    94.                     QualitySettings.currentLevel = QualityLevel.Good;
    95.                 }
    96.                 if(GUI.Button(new Rect(Screen.width /2 + 150,Screen.height /2 + 200,250,50), "Beautiful")){
    97.                     QualitySettings.currentLevel = QualityLevel.Beautiful;
    98.                 }
    99.                 if(GUI.Button(new Rect(Screen.width /2 + 150,Screen.height /2 + 250,250,50), "Fantastic")){
    100.                     QualitySettings.currentLevel = QualityLevel.Fantastic;
    101.                 }
    102.            
    103.                 if(Input.GetKeyDown("escape")){
    104.                     showGraphicsDropDown = false;
    105.                 }
    106.             }
    107.        
    108.             //Make quit game button
    109.             if (GUI.Button (new Rect (Screen.width /2 - 100,Screen.height /2 + 50,250,50), "Quit Game")){
    110.                 Application.Quit();
    111.             }
    112.         }
    113.     }
    114. }
    You'll want to change line 22 on the PauseMenu script to look for whatever you've called your FPSController. I was just using the default for testing. Works for me though, hope it does for you as well!
     
  41. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Hello !

    Thanks, i created 2 scripts so :

    - FirstPersonController in C# which is the FPSController
    - PauseMenu in C# which is the PauseMenu

    I put them on the player and i deactivated the previous FPSController.
    I didn't touch any options of the scripts after i put them on the player.

    Is that correct ?
    If so, camera still moving... But got the PauseMenu in C# is amazing !! I'm gonna search a solution with that.
     
    krougeau likes this.
  42. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    OH IT IS WORKING OMG !!! GENIUS KROUGEAU STRIKE AGAIN !!!

    I just needed to change the line 22 to Player, forgot about that !!!

    YOU'RE AMAZING *_*
     
    krougeau likes this.
  43. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    LOL! Thanks ;) So glad it's working for ya! As for "genius", I'm still more of a floundering noobling myself, haha, but I'm working on it as much as I can :p Have fun!!
     
  44. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Haha xD Well you're better than me, that's for sure !

    Thank for your help !! <3
     
    krougeau likes this.
  45. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I'm just going to throw in my 2 cents on the C#/JS question, and say definitely learn C#. Unity's own statistics indicate that 75% of Unity users use C#, which means that sharing code and getting assistance will become much easier, which IMO negates whatever simplicity advantage JS has.

    And C#'s utility extends far, far beyond just Unity, and this is an ENORMOUS advantage for C#. If you need an algorithm (or language feature) that is not Unity-specific, you'll have a much wider world of code to choose from, since C# is used in so many other places - not the least of which is the absolutely fantastic MSDN documentation of the language. Need to access the global system time? Bam. Need serialization (writing stuff to disk in an easy-to-reimport way)? They've got a good writeup. This level of resources simply don't exist for JS. (There's plenty of sample code for JavaScript on the web, but that's for web javascript, which has very little to do with Unity's flavor of javascript and usually isn't compatible. Unity's Javascript is unique to Unity.)

    C# has language features like properties and delegates that are a joy to work with, and easy to find sample code for on the web, but are not as commonly used in Unity, so that sample code is usually not going to be Unity-specific. Those features exist in Javascript, but TBH I haven't a clue where you would look to find out the syntax for them. With C#, they are very easy to google - just search for "C# delegates" or whatever language feature you want, and 90% of the time you can just click on the MSDN link and be done with it.

    You're (presumably) just a student or a hobbyist now, but if you have any plans on becoming a professional programmer, C# will be your road to doing that. This is true even if you don't plan on leaving the Unity-sphere - most companies I've worked for are either indifferent to the choice of scripting language used, or are strictly C#.

    And C# experience in Unity is directly relatable to the rest of the coding world, even more so the more you venture outside of Unity's API into the .NET classes. And the inverse is true, too - if you are the project leader and you need, say, an expert in networking, you'll be able to find one that knows C# - and he doesn't need to have ever touched Unity to be useful in your project.

    There are more options for good IDE's and text editors with C# for this same reason. You've got MonoDevelop of course, but many of the alternatives (like the currently-in-beta but pretty fantastic Visual Studio Code) understand C# but not Unity's Javascript.

    And all of that is ignoring my own preferences for C#'s syntax and behavior over JS's, which are not insignificant, but are a little more subjective. The simple fact is that C# is objectively far more useful explicitly because of the larger, more experienced, more professional community surrounding it.
     
    krougeau likes this.
  46. naruto-hokager

    naruto-hokager

    Joined:
    May 24, 2015
    Posts:
    28
    Yeah that's the secondary fact why i like more C# than JScript now. I like too the method of understanding of C# which we need to indicate the type of variable. (Thanks to Krougeau !)

    Thank you for your review ! :)
     
    krougeau likes this.
  47. SilverM

    SilverM

    Joined:
    Jun 29, 2015
    Posts:
    5
    what did you change in that line to player. Not working for me at all
     
  48. SilverM

    SilverM

    Joined:
    Jun 29, 2015
    Posts:
    5
    Well i changed it to what it have mine named but it isnt working. No errors, just not locking the camera
     
  49. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Just to be sure, you're using the two scripts above and have changed the line in the PauseMenu script that reads
    Code (CSharp):
    1. fps = GameObject.Find("FPSController");
    to
    Code (CSharp):
    1. fps = GameObject.Find("WHATEVER-YOU-RENAMED-THE-FPS-CONTROLLER");
    and it's not locating the renamed FPS Controller? I'm left to assume that you've missed a vital step somewhere along the way, because it's been tested and works just fine. Please elaborate on your setup and I'll help as time allows. Thanks!
     
    SilverM likes this.
  50. SilverM

    SilverM

    Joined:
    Jun 29, 2015
    Posts:
    5
    Thank you for your help. Its the little things. I did not match the controller names appropriately and it works now. Nicely done boys.

    -Silver
     
    krougeau likes this.