Search Unity

Gun Script [C#]

Discussion in 'Scripting' started by nostertag, Feb 18, 2018.

  1. nostertag

    nostertag

    Joined:
    Feb 18, 2018
    Posts:
    2
    Hello there,

    I tried to create a kind of gun script in Unity, I followed orders from a youtube video (which is in german:
    )

    I already "translated" the script from Javascript to C#.

    For some reason, unity says that there are errors in the script, but my script editing program doesn't...
    can someone please help me? Thanks!

    Script-------------
    Code (CSharp):
    1. // Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
    2. // Do test the code! You usually need to change a few small bits.
    3. using UnityEngine.UI;
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class ACE_23 : MonoBehaviour {
    8.  
    9.  
    10.  
    11. GameObject gun;
    12. GameObject soundShot;
    13.  
    14. float distance;
    15. float maxDistance = 50;
    16. int damage = 25;
    17. static bool isAiming = false;
    18.  
    19. float fireSpeed = 20;
    20. float waitTilNextFire = 0;
    21.  
    22. bool bulletHole = GameObject;
    23.  
    24. bool reloading = false;
    25. Animation reloadAnimation;
    26. AudioSource reloadingSound;
    27.  
    28. int maxMag = 36;
    29. int currentMag = 36;
    30. int magAfterEmpty = 35;
    31. int maxAmmo = 175;
    32. int currentAmmo = 175;
    33. Text showAmmo;
    34.  
    35. float spreadFactor;
    36.  
    37. Transform muzzleFlashSpawn;
    38. GameObject muzzleFlashPrefab;
    39.  
    40.  
    41.  
    42. void  Update (){
    43.         if(Input.GetButton("Fire2") && reloading == false)
    44.         {
    45.             isAiming = !isAiming;
    46.          
    47.             if(isAiming == true)
    48.             {
    49.                 GameObject.Find("ACE_23").GetComponent<Animation>().Play("aimdownsight_on");
    50.             }
    51.          
    52.             else
    53.             {
    54.                 GameObject.Find("ACE_23").GetComponent<Animation>().Play("aimdownsight_off");
    55.             }
    56.         }
    57.          
    58.          
    59.         showAmmo.gameObject.SetActive(true);
    60.         showAmmo.text = "Ammo: " + currentMag.ToString() + "/" + currentAmmo.ToString();
    61.      
    62.      
    63.         if(Input.GetButton("Fire1") && waitTilNextFire <= 0 && currentMag >= 1 && !reloading)
    64.         {
    65.             if(isAiming == true)
    66.             {
    67.                 GameObject.Find("ACE_23").GetComponent.Animation>(Play("Shoot_ACE23_ads"));
    68.                 spreadFactor = 10;
    69.             }
    70.          
    71.             else
    72.             {
    73.                 GameObject.Find("ACE_23").GetComponent.Animation>(Play("Shoot_ACE23_hip"));
    74.                 spreadFactor = 50;
    75.             }
    76.          
    77.             bool muzzleFlash= Instantiate (muzzleFlashPrefab, muzzleFlashSpawn.position, muzzleFlashSpawn.rotation);
    78.             muzzleFlash.transform.parent = gun.transform;
    79.          
    80.             bool shotSound= Instantiate (shotSound, muzzleFlashSpawn.position, muzzleFlashSpawn.rotation);
    81.             shotSound.transform.parent = gun.transform;
    82.          
    83.             currentMag --;
    84.             waitTilNextFire = 1;
    85.          
    86.             RaycastHit hit;
    87.             pos= Vector3(Screen.width / 2, Screen.height / 2, Camera.main.nearClipPlane);
    88.             pos += Random.insideUnitCircle * spreadFactor;
    89.             ray= Camera.main.ScreenPointToRay(pos);
    90.          
    91.             if(Physics.Raycast (ray, hit, maxDistance))
    92.             {
    93.                 distance = hit.distance;
    94.                 if(distance < maxDistance)
    95.                 {
    96.                     hit.transform.SendMessage("GunDamage", damage, SendMessageOptions.DontRequireReceiver);
    97.                 }
    98.              
    99.                 if(distance < maxDistance && hit.transform.tag == "Level_Object")
    100.                 {
    101.                     Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
    102.                 }
    103.             }
    104.         }
    105.      
    106.         waitTilNextFire -= Time.deltaTime * fireSpeed;
    107.      
    108.         if(waitTilNextFire <= 0)
    109.         {
    110.             waitTilNextFire = 0;
    111.         }
    112.      
    113.         //AMMO
    114.      
    115.         if(currentMag > maxMag)
    116.         {
    117.             currentMag = maxMag;
    118.         }
    119.      
    120.         if(currentAmmo > maxAmmo)
    121.         {
    122.             currentAmmo = maxAmmo;
    123.         }
    124.      
    125.         if(Input.GetButtonDown(KeyCode.R) && !reloading && currentMag < maxMag && currentAmmo > 0)
    126.         {
    127.             reloading = true;
    128.             isAiming = false;
    129.             GameObject.Find("ACE_23").GetComponent.Animation>(Play("ace_23_reload"));
    130.             reloadingSound.Play();
    131.         }
    132.      
    133.         if(Input.GetButtonDown("Fire1") && !reloading && currentMag == 0)
    134.         {
    135.             GameObject.Find("ACE_23").GetComponent.Animation>(Play("ace_23_empty"));
    136.         }
    137.  
    138.         if(reloading == true && !reloadAnimation.IsPlaying("ace_23_reload"))
    139.         {
    140.             if(currentMag <= 0)
    141.             {
    142.                 if(currentAmmo >= maxMag - currentMag)
    143.                 {
    144.                     currentAmmo -= magAfterEmpty - currentMag;
    145.                     currentMag = magAfterEmpty;
    146.                 }
    147.              
    148.                 if(currentAmmo >= maxMag - currentMag)
    149.                 {
    150.                     currentAmmo -= maxMag - currentMag;
    151.                     currentMag = maxMag;
    152.                 }
    153.              
    154.                 if(currentAmmo < maxMag - currentMag)
    155.                 {
    156.                     currentMag += currentAmmo;
    157.                     currentAmmo = 0;
    158.                 }
    159.             }
    160.          
    161.             reloading = false;
    162.         }
    163.     }
    164.  
    165. }
     
    Last edited: Feb 18, 2018
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Please look at this page for how to share code on the forums: https://forum.unity3d.com/threads/using-code-tags-properly.143875/

    Please think about how you just wrote to users saying "hey, I got an error".. but i don't want to tell you what it is! Here are 100 lines of code, can you help me out? :p Sorry... sigh.

    I see a few spots that say "GetComponent.Animation" instead of "GetComponent<Animation>". Perhaps if you edit your post with code tags, and write the line # on which the error is, someone can help you.

    Also, if you click/double click on the error in the console, it should bring you to the error in your IDE, and perhaps you can figure it out (or just post the line in question, instead of everything).
     
  3. nostertag

    nostertag

    Joined:
    Feb 18, 2018
    Posts:
    2
    First of all, thanks. I'm new in this forum as new to the forum as I am to unity, so I didn't really know how to edit the script.
    Next, there is the problem that I'm not even told where the mistake is...
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hm. Can you paste the error here? the full msg?