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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Convert Unity 3/4 script to 5??

Discussion in 'Scripting' started by Kameron-Schwab, Oct 23, 2015.

  1. Kameron-Schwab

    Kameron-Schwab

    Joined:
    Sep 16, 2015
    Posts:
    33
    I've downloaded a Unity 4 script and it uses the old FPS script called "CharacterMotor"

    Here is a crouching code that I downloaded that uses it and I'm having trouble converting/upgrading it to Unity 5 code/scripting

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var walkSpeed : float = 7; //Regular speed
    4. var crouchSpeed : float = 3; //Speed while crouching
    5.  
    6. private var charMotor : CharacterMotor;
    7. private var charController : CharacterController;
    8. private var theTransform : Transform;
    9. private var charHeight : float; //Initial height
    10.  
    11. function Start ()
    12. {
    13.     charMotor = GetComponent(CharacterMotor);
    14.     theTransform = transform;
    15.     charController = GetComponent(CharacterController);
    16.     charHeight = charController.height;
    17. }
    18.  
    19. function Update ()
    20. {
    21.     var h = charHeight;
    22.     var speed = walkSpeed;
    23.    
    24.     if (Input.GetKey("c"))
    25.     {
    26.         h = charHeight*0.5;
    27.         speed = crouchSpeed;
    28.     }
    29.    
    30.     charMotor.movement.maxForwardSpeed = speed; // Setting the max speed
    31.     var lastHeight = charController.height; //Stand up/crouch smoothly
    32.     charController.height = Mathf.Lerp(charController.height, h, 5*Time.deltaTime);
    33.     theTransform.position.y += (charController.height-lastHeight)/2; //Fix vertical position
    34. }
    Also got another error with respawning... I also have a respawn script and I get a error of "MouseLook" not existing. Also I have two.
    First one:
    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4. var lookAround01 : MouseLook;
    5. var lookAround02 : MouseLook;
    6. var charController : CharacterController;
    7. //var sprintScript : SprintAndCrouch;
    8.  
    9. static var playerIsDead = false;
    10.  
    11. function Start ()
    12. {
    13.     lookAround01 = gameObject.GetComponent(MouseLook);
    14.     lookAround02 = GameObject.Find("MainCamera").GetComponent(MouseLook);
    15.     charController = gameObject.GetComponent(CharacterController);
    16.     //sprintScript = gameObject.GetComponent(SprintAndCrouch);
    17. }
    18.  
    19. function Update ()
    20. {
    21.     if (playerIsDead == true)
    22.     {
    23.         lookAround01.enabled = false;
    24.         lookAround02.enabled = false;
    25.         //sprintScript.enabled = false;
    26.         charController.enabled = false;
    27.     }
    28. }
    29.  
    30. function OnGUI ()
    31. {
    32.     if (playerIsDead == true)
    33.     {
    34.         if (GUI.Button(Rect(Screen.width*0.5-50, 200-20, 100, 40), "Respawn"))
    35.         {
    36.             RespawnPlayer();
    37.         }
    38.        
    39.         if (GUI.Button(Rect(Screen.width*0.5-50, 240, 100, 40), "Menu"))
    40.         {
    41.             Debug.Log("Return to Menu");
    42.         }
    43.     }
    44. }
    45.  
    46. function RespawnPlayer ()
    47. {
    48.     Debug.Log("Respawn Player");
    49. }
    50.  
    51. @script RequireComponent(CharacterController)
    Second One:
    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4. var lookAround01 : MouseLook;
    5. var lookAround02 : MouseLook;
    6. var charController : CharacterController;
    7.  
    8. var respawnTransform : Transform;
    9.  
    10. static var playerIsDead = false;
    11.  
    12. function Start ()
    13. {
    14.     lookAround01 = gameObject.GetComponent(MouseLook);
    15.     lookAround02 = GameObject.Find("MainCamera").GetComponent(MouseLook);
    16.     charController = gameObject.GetComponent(CharacterController);
    17. }
    18.  
    19. function Update ()
    20. {
    21.     if (playerIsDead == true)
    22.     {
    23.         lookAround01.enabled = false;
    24.         lookAround02.enabled = false;
    25.         charController.enabled = false;
    26.     }
    27. }
    28.  
    29. function OnGUI ()
    30. {
    31.     if (playerIsDead == true)
    32.     {
    33.         if (GUI.Button(Rect(Screen.width*0.5-50, 200-20, 100, 40), "Respawn"))
    34.         {
    35.             RespawnPlayer();
    36.         }
    37.        
    38.         if (GUI.Button(Rect(Screen.width*0.5-50, 240, 100, 40), "Menu"))
    39.         {
    40.             Debug.Log("Return to Menu");
    41.         }
    42.     }
    43. }
    44.  
    45. function RespawnPlayer ()
    46. {
    47.     transform.position = respawnTransform.position;
    48.     transform.rotation = respawnTransform.rotation;
    49.     gameObject.SendMessage("RespawnStats");
    50.     lookAround01.enabled = true;
    51.     lookAround02.enabled = true;
    52.     charController.enabled = true;
    53.     playerIsDead = false;
    54.     Debug.Log("Player has respawned");
    55. }
    56.  
    57. @script RequireComponent(CharacterController)
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,998
    Can you post the errors from console and error line numbers?
     
    Kameron-Schwab likes this.
  3. Kameron-Schwab

    Kameron-Schwab

    Joined:
    Sep 16, 2015
    Posts:
    33
    Yes, sir here you go. Also I have 47 errors but that's because of all of this..

     
  4. Kameron-Schwab

    Kameron-Schwab

    Joined:
    Sep 16, 2015
    Posts:
    33
  5. Kameron-Schwab

    Kameron-Schwab

    Joined:
    Sep 16, 2015
    Posts:
    33