Search Unity

In Need of Someone to Improve my Scripts - JavaScript

Discussion in 'Scripting' started by Deleted User, Jul 3, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hi, I am somewhat of a noob when it comes to coding, but I can get the job done. I'm never sure if my code is as efficient and as clean as it could be. I was wondering if I could have someone help clean and correct my scripts as I go on with my games. I would love the help if you have the spare time. If you want to talk about it, email me at duskerstudios@gmail.com. Here's a script I really need to get corrected and made more efficient:

    Code (JavaScript):
    1.  
    2. var projectile : Rigidbody;
    3. var fireRate : float = 0.5;
    4. var projectileSpeed = 20;
    5. var ammo : float;
    6. var maxAmmo : float;
    7. var animator : Animator;
    8. var style : GUIStyle;
    9. var reloadTime : float = 1.0;
    10. var gunFire : AudioClip;
    11. var gunReload : AudioClip;
    12. private var reloadTimer : float;
    13. var isReloading : boolean = false;
    14. private var nextFire = 0.0;
    15.  
    16. function Start ()
    17. {
    18.     ammo = maxAmmo;
    19.     reloadTimer = 0;
    20. }
    21.  
    22. function Update ()
    23. {
    24.     reloadTimer -= Time.deltaTime;
    25.  
    26.     if(Input.GetButtonDown("Fire1") && (Time.time <= nextFire))
    27.     {
    28.         // Do nothing
    29.     }
    30.  
    31.     if(Input.GetButtonDown("Fire1") && (Time.time > nextFire))
    32.     {
    33.         if(isReloading == false)
    34.         {
    35.             nextFire = Time.time + fireRate;
    36.             Fire();
    37.             animator.SetBool("IsFiring", true);
    38.         }
    39.         else if(isReloading == true)
    40.         {
    41.             // Do nothing
    42.         }  
    43.     }
    44.     else
    45.     {
    46.         animator.SetBool("IsFiring", false);
    47.     }
    48.  
    49.     if(Input.GetKeyDown(KeyCode.R))
    50.     {
    51.         Reload();
    52.         animator.SetBool("IsReloading", true);
    53.     }
    54.     else
    55.     {
    56.         animator.SetBool("IsReloading", false);
    57.     }
    58.  
    59.     if(reloadTimer > 0)
    60.     {
    61.         isReloading = true;
    62.     }
    63.     else
    64.     {
    65.         isReloading = false;
    66.     }
    67.  
    68.     if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
    69.     {
    70.         animator.SetBool("IsWalking", true);
    71.     }
    72.     else
    73.     {
    74.         animator.SetBool("IsWalking", false);
    75.     }
    76. }
    77.  
    78. function Fire ()
    79. {
    80.     GetComponent.<AudioSource>().PlayOneShot(gunFire);
    81.     var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, Quaternion.identity);
    82.     ammo -= 1;
    83.     instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, projectileSpeed ) );
    84.     CancelInvoke();
    85. }
    86.  
    87. function OnGUI ()
    88. {  
    89.     GUI.Label(Rect(1400, 600, 100, 20), ammo.ToString()+"/"+maxAmmo.ToString(), style);
    90.     GUI.Label(Rect(1400, 575, 100, 20), isReloading.ToString(), style);
    91. }
    92.  
    93. function Reload ()
    94. {
    95.     GetComponent.<AudioSource>().PlayOneShot(gunReload);
    96.     ammo = maxAmmo;
    97.     reloadTimer = reloadTime;
    98. }
    Everything is operational, it just needs to be improved. The help would be appreciated. Thanks!
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Oi just dis a quick scan.
    Everything looks quite clean. The only thing I would suggest is to not use get component and instead create a variable to store the component. Also, as far as I know, onGui is quite a costly piece. Any reason you are not using Unity Ui?