Search Unity

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

How to create single fire ?

Discussion in 'Scripting' started by ggsnip, Nov 28, 2014.

  1. ggsnip

    ggsnip

    Joined:
    Nov 28, 2014
    Posts:
    3
    Hi !:cool: i have a script for my weapon and i want insert a single fire in this script :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class ReloadSound
    6. {
    7.     public string name = "Mag out";
    8.     public AudioClip clip;
    9.     public float length;
    10. }
    11.  
    12. public class Weapon : MonoBehaviour
    13. {
    14.     public Animation anim;
    15.     public AnimationClip fireAnim;
    16.     public AnimationClip reloadAnim;
    17.     public AnimationClip reloadEmptyAnim;
    18.     public AnimationClip drawAnim;
    19.  
    20.     #region bools
    21.     public bool reloading;
    22.     public bool[] canAims;
    23.     private bool canAim;
    24.     public bool[] canReloads;
    25.     private bool canReload;
    26.     public bool[] canFires;
    27.     private bool canFire;
    28.     #endregion
    29.  
    30.     #region stats
    31.     public float fireRate = 0.1f;
    32.     public float timer = 0;
    33.     [SerializeField]
    34.     protected int bulletsLeft = 30;
    35.     [SerializeField]
    36.     protected int bulletsPerMag = 30;
    37.     [SerializeField]
    38.     protected int magsLeft = 10;
    39.     public float range = 2000;
    40.     public float damageMin = 10;
    41.     public float damageMax = 20;
    42.     public Transform bulletGo;
    43.     public LayerMask hitLayers;
    44.     public GameObject blood;
    45.     public GameObject concrete;
    46.     public GameObject wood;
    47.     public GameObject metal;
    48.     public GameObject dirt;
    49.     #endregion
    50.  
    51.     #region readOnly
    52.     public int bulletsLeftRead = 30;
    53.     public int bulletsPerMagRead = 30;
    54.     public int magsLeftRead = 10;
    55.     #endregion
    56.  
    57.  
    58.     #region components
    59.     public CharacterValues cv;
    60.     public PlayerAnimations pa;
    61.     #endregion
    62.  
    63.     #region sound
    64.     public AudioSource localSource;
    65.     public AudioClip fireSound;
    66.     public ReloadSound[] drawSound;
    67.     public ReloadSound[] reloadSounds;
    68.     public ReloadSound[] reloadSoundsEmpty;
    69.     #endregion
    70.  
    71.     #region ads
    72.     public Camera cam;
    73.     public bool aiming;
    74.     public float hipFov = 75;
    75.     public float aimFov = 55;
    76.     private float curFov = 75;
    77.     public Vector3 hipPos;
    78.     public Vector3 crouchPos;
    79.     public Vector3 aimPos;
    80.     private Vector3 curPos;
    81.     #endregion
    82.  
    83.     #region recoil
    84.     public Transform camKB;
    85.     public Transform wepKB;
    86.     public float minKB;
    87.     public float maxKB;
    88.     public float minKBSide;
    89.     public float maxKBSide;
    90.     public float returnSpeed = 5f;
    91.     #endregion
    92.  
    93.     #region muzzle
    94.     public GameObject muzzle;
    95.     #endregion
    96.  
    97.     #region crosshair
    98.     public float sizeMultiplier = 1f;
    99.     public float aimSpread;
    100.     public float basicSpread = 30;
    101.     public float maximumSpread = 100;
    102.     public float spreadReturnTime = 5;
    103.     public float spreadAddPerShot = 5;
    104.     public float spreadTemp;
    105.     private float spread = 30;
    106.  
    107.     //Crosshair Textures
    108.     public Texture2D crosshairFirstModeHorizontal;
    109.     public Texture2D crosshairFirstModeVertical;
    110.  
    111.     #endregion
    112.  
    113.     #region private
    114.     private Vector2 pivot;
    115.     #endregion
    116.  
    117.     #region hitMark
    118.     public Texture2D tex;
    119.     public float size = 32;
    120.     private float hitAlpha;
    121.     public AudioClip hitMarkerSound;
    122.     #endregion
    123.  
    124.  
    125.     void Start()
    126.     {
    127.         muzzle.SetActive(false);
    128.         if (networkView.isMine)
    129.         {
    130.             spreadTemp = basicSpread;
    131.             spread = basicSpread;
    132.             StartCoroutine(CheckBools());
    133.             StartCoroutine(Draw());
    134.         }
    135.         else
    136.         {
    137.             this.enabled = false;
    138.         }
    139.     }
    140.  
    141.     void Update()
    142.     {
    143.         if (networkView.isMine)
    144.         {
    145.             if (hitAlpha > 0) hitAlpha -= Time.deltaTime;
    146.             spread = Mathf.Clamp(spread, 0, maximumSpread);
    147.             if (aiming) spread = aimSpread;
    148.             else spread = Mathf.Lerp(spread, spreadTemp + cv.velMag * 2, Time.deltaTime * 8);
    149.             if (spreadTemp > basicSpread) spreadTemp -= Time.deltaTime * spreadReturnTime;
    150.             pivot = new Vector2(Screen.width / 2, Screen.height / 2);
    151.             bulletsLeftRead = bulletsLeft;
    152.             bulletsPerMagRead = bulletsPerMag;
    153.             magsLeftRead = magsLeft;
    154.             camKB.localRotation = Quaternion.Lerp(camKB.localRotation, Quaternion.identity, Time.deltaTime * returnSpeed);
    155.             wepKB.localRotation = Quaternion.Lerp(wepKB.localRotation, Quaternion.identity, Time.deltaTime * returnSpeed);
    156.             cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, curFov, Time.deltaTime * 10);
    157.             transform.localPosition = Vector3.Lerp(transform.localPosition, curPos, Time.deltaTime * 10);
    158.             if (Screen.lockCursor)
    159.                 CheckInput();
    160.             canReloads[1] = true;
    161.             canAims[1] = !cv.running;
    162.             canFires[1] = !cv.running;
    163.             if (aiming)
    164.             {
    165.                 curFov = aimFov;
    166.                 curPos = aimPos;
    167.             }
    168.             else
    169.             {
    170.                 curFov = hipFov;
    171.                 if (cv.state == 0)
    172.                 {
    173.                     curPos = hipPos;
    174.                 }
    175.                 else if (cv.state == 1)
    176.                 {
    177.                     curPos = crouchPos;
    178.                 }
    179.             }
    180.  
    181.             if (!canAim) aiming = false;
    182.         }
    183.         else
    184.         {
    185.             this.enabled = false;
    186.         }
    187.     }
    188.  
    189.     void OnGUI()
    190.     {
    191.         if (networkView.isMine)
    192.         {
    193.  
    194.             float w = crosshairFirstModeHorizontal.width;
    195.             float h = crosshairFirstModeHorizontal.height;
    196.             Rect position1 = new Rect((Screen.width + w) / 2 + (spread * sizeMultiplier), (Screen.height - h) / 2, w, h);
    197.             Rect position2 = new Rect((Screen.width - w) / 2, (Screen.height + h) / 2 + (spread * sizeMultiplier), w, h);
    198.             Rect position3 = new Rect((Screen.width - w) / 2 - (spread * sizeMultiplier) - w, (Screen.height - h) / 2, w, h);
    199.             Rect position4 = new Rect((Screen.width - w) / 2, (Screen.height - h) / 2 - (spread * sizeMultiplier) - h, w, h);
    200.             if (!aiming)
    201.             {
    202.                 GUI.DrawTexture(position1, crosshairFirstModeHorizontal);     //Right
    203.                 GUI.DrawTexture(position2, crosshairFirstModeVertical);     //Up
    204.                 GUI.DrawTexture(position3, crosshairFirstModeHorizontal);     //Left
    205.                 GUI.DrawTexture(position4, crosshairFirstModeVertical);        //Down
    206.             }
    207.  
    208.             GUI.color = new Color(1, 1, 1, hitAlpha);
    209.             GUI.DrawTexture(new Rect((Screen.width - size) / 2, (Screen.height - size) / 2, size, size), tex);
    210.  
    211.         }
    212.     }
    213.  
    214.     void CheckInput()
    215.     {
    216.         aiming = (canAim && Input.GetKey(KeyCode.Mouse1));
    217.         if (!reloading && Time.time > timer && canFire && Input.GetKey(KeyCode.Mouse0) && bulletsLeft > 0 && Screen.lockCursor)
    218.         {
    219.             FireOneShot();
    220.         }
    221.         if (!reloading && canReload && magsLeft > 0 && Input.GetKeyDown(KeyCode.R) && Screen.lockCursor)
    222.         {
    223.             reloading = true;
    224.             StartCoroutine(Reload());
    225.         }
    226.     }
    227.  
    228.     void FireOneShot()
    229.     {
    230.         spreadTemp += spreadAddPerShot;
    231.         timer = Time.time + fireRate;
    232.         anim.Rewind(fireAnim.name);
    233.         anim.Play(fireAnim.name);
    234.         localSource.clip = fireSound;
    235.         localSource.PlayOneShot(fireSound);
    236.         StartCoroutine(MuzzleFlash());
    237.         StartCoroutine(Kick3(camKB, new Vector3(-Random.Range(minKB, maxKB), Random.Range(minKBSide, maxKBSide), 0), 0.1f));
    238.         StartCoroutine(Kick3(wepKB, new Vector3(-Random.Range(minKB, maxKB), Random.Range(minKBSide, maxKBSide), 0), 0.1f));
    239.  
    240.         float actualSpread = Random.Range(-spread, spread);
    241.         //Vector3 position = new Vector3(bulletGo.position.x - actualSpread, bulletGo.position.y - actualSpread, bulletGo.position.z);
    242.         Vector3 direction = gameObject.transform.TransformDirection(new Vector3(Random.Range(-0.01f, 0.01f) * spread, Random.Range(-0.01f, 0.01f) * spread, 1));
    243.         RaycastHit hit2;
    244.         if (Physics.Raycast(bulletGo.position, direction, out hit2, range, hitLayers))
    245.         {
    246.             OnHit(hit2);
    247.         }
    248.         bulletsLeft--;
    249.     }
    250.  
    251.     void DoHitMark()
    252.     {
    253.         hitAlpha = 2;
    254.         audio.PlayOneShot(hitMarkerSound, 1f);
    255.     }
    256.  
    257.     void OnHit(RaycastHit hit)
    258.     {
    259.         if (hit.rigidbody)
    260.         {
    261.             hit.rigidbody.AddForceAtPosition(2000 * bulletGo.forward, hit.point);
    262.         }
    263.         if (hit.transform.tag == "Player")
    264.         {
    265.             Instantiate(blood, hit.point, Quaternion.identity);
    266.             DoHitMark();
    267.             if (hit.transform.root.networkView)
    268.                 hit.transform.root.networkView.RPC("ApplyDamage", RPCMode.AllBuffered, Random.Range(damageMin, damageMax), 1);
    269.         }
    270.         else
    271.         {
    272.             if (hit.transform.tag == "Wood")
    273.             {
    274.                 GameObject theObj = Instantiate(wood, hit.point + hit.normal * 0.01f, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
    275.                 theObj.transform.parent = hit.transform;
    276.             }
    277.             else if (hit.transform.tag == "Metal")
    278.             {
    279.                 GameObject theObj = Instantiate(metal, hit.point + hit.normal * 0.01f, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
    280.                 theObj.transform.parent = hit.transform;
    281.             }
    282.             else if (hit.transform.tag == "Dirt")
    283.             {
    284.                 GameObject theObj = Instantiate(dirt, hit.point + hit.normal * 0.01f, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
    285.                 theObj.transform.parent = hit.transform;
    286.             }
    287.             else
    288.             {
    289.                 GameObject theObj = Instantiate(concrete, hit.point + hit.normal * 0.01f, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
    290.                 theObj.transform.parent = hit.transform;
    291.             }
    292.         }
    293.     }
    294.  
    295.     IEnumerator Kick3(Transform goTransform, Vector3 kbDirection, float time)
    296.     {
    297.         Quaternion startRotation = goTransform.localRotation;
    298.         Quaternion endRotation = goTransform.localRotation * Quaternion.Euler(kbDirection);
    299.         float rate = 1.0f / time;
    300.         var t = 0.0f;
    301.         while (t < 1.0f)
    302.         {
    303.             t += Time.deltaTime * rate;
    304.             goTransform.localRotation = Quaternion.Slerp(startRotation, endRotation, t);
    305.             yield return null;
    306.         }
    307.     }
    308.  
    309.     IEnumerator Reload()
    310.     {
    311.         reloading = true;
    312.         canAims[0] = false;
    313.         canFires[0] = false;
    314.         canReloads[0] = false;
    315.         if (bulletsLeft > 0)
    316.         {
    317.             StartCoroutine(ReloadingSound(reloadSounds));
    318.             anim.Play(reloadAnim.name);
    319.             yield return new WaitForSeconds(reloadAnim.length);
    320.             bulletsLeft = bulletsPerMag + 1;
    321.             magsLeft--;
    322.         }
    323.         else
    324.         {
    325.             StartCoroutine(ReloadingSound(reloadSoundsEmpty));
    326.             anim.Play(reloadEmptyAnim.name);
    327.             yield return new WaitForSeconds(reloadEmptyAnim.length);
    328.             bulletsLeft = bulletsPerMag;
    329.             magsLeft--;
    330.         }
    331.         canAims[0] = true;
    332.         canFires[0] = true;
    333.         canReloads[0] = true;
    334.         reloading = false;
    335.     }
    336.  
    337.     IEnumerator ReloadingSound(ReloadSound[] theSound)
    338.     {
    339.         foreach (ReloadSound lol in theSound)
    340.         {
    341.             yield return new WaitForSeconds(lol.length);
    342.             localSource.clip = lol.clip;
    343.             localSource.Play();
    344.         }
    345.     }
    346.  
    347.     IEnumerator CheckBools()
    348.     {
    349.         CheckAim();
    350.         CheckReload();
    351.         CheckFire();
    352.         yield return new WaitForSeconds(0.1f);
    353.         StartCoroutine(CheckBools());
    354.     }
    355.  
    356.     IEnumerator MuzzleFlash()
    357.     {
    358.         muzzle.SetActive(true);
    359.         yield return new WaitForSeconds(0.1f);
    360.         muzzle.SetActive(false);
    361.     }
    362.  
    363.     void CheckAim()
    364.     {
    365.         canAim = false;
    366.         foreach (bool lol in canAims)
    367.         {
    368.             if (!lol) return;
    369.         }
    370.         canAim = true;
    371.     }
    372.  
    373.     void CheckReload()
    374.     {
    375.         canReload = false;
    376.         foreach (bool lol in canReloads)
    377.         {
    378.             if (!lol) return;
    379.         }
    380.         canReload = true;
    381.     }
    382.  
    383.     void CheckFire()
    384.     {
    385.         canFire = false;
    386.         foreach (bool lol in canFires)
    387.         {
    388.             if (!lol) return;
    389.         }
    390.         canFire = true;
    391.     }
    392.  
    393.     IEnumerator Draw()
    394.     {
    395.         canAims[0] = false;
    396.         canFires[0] = false;
    397.         canReloads[0] = false;
    398.         //localSource.clip = drawSound;
    399.         //localSource.Play();
    400.         StartCoroutine(ReloadingSound(drawSound));
    401.         anim.Play(drawAnim.name);
    402.         yield return new WaitForSeconds(drawAnim.length);
    403.         canAims[0] = true;
    404.         canFires[0] = true;
    405.         canReloads[0] = true;
    406.     }
    407. }
    please help me :(

    PS: i'm french and i'm sorry for the bad english :p and i don't if i'm in the good sectiono_O
     
  2. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    This is untested, but the direction is ok i think.

    I added two new variables :
    public bool singleShotMode = false;
    private bool waitingForMouseUp = false;

    and added a little code in the Update() and CheckInput() functions.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [System.Serializable]
    4. public class ReloadSound
    5. {
    6.     public string name = "Mag out";
    7.     public AudioClip clip;
    8.     public float length;
    9. }
    10. public class Weapon : MonoBehaviour
    11. {
    12.     public Animation anim;
    13.     public AnimationClip fireAnim;
    14.     public AnimationClip reloadAnim;
    15.     public AnimationClip reloadEmptyAnim;
    16.     public AnimationClip drawAnim;
    17.     #region bools
    18.     public bool reloading;
    19.     public bool[] canAims;
    20.     private bool canAim;
    21.     public bool[] canReloads;
    22.     private bool canReload;
    23.     public bool[] canFires;
    24.     private bool canFire;
    25.     #endregion
    26.     #region stats
    27.     public float fireRate = 0.1f;
    28.     public float timer = 0;
    29.     [SerializeField]
    30.     protected int bulletsLeft = 30;
    31.     [SerializeField]
    32.     protected int bulletsPerMag = 30;
    33.     [SerializeField]
    34.     protected int magsLeft = 10;
    35.     public float range = 2000;
    36.     public float damageMin = 10;
    37.     public float damageMax = 20;
    38.     public bool singleShotMode = false;
    39.     private bool waitingForMouseUp = false;
    40.     public Transform bulletGo;
    41.     public LayerMask hitLayers;
    42.     public GameObject blood;
    43.     public GameObject concrete;
    44.     public GameObject wood;
    45.     public GameObject metal;
    46.     public GameObject dirt;
    47.     #endregion
    48.     #region readOnly
    49.     public int bulletsLeftRead = 30;
    50.     public int bulletsPerMagRead = 30;
    51.     public int magsLeftRead = 10;
    52.     #endregion
    53.     #region components
    54.     public CharacterValues cv;
    55.     public PlayerAnimations pa;
    56.     #endregion
    57.     #region sound
    58.     public AudioSource localSource;
    59.     public AudioClip fireSound;
    60.     public ReloadSound[] drawSound;
    61.     public ReloadSound[] reloadSounds;
    62.     public ReloadSound[] reloadSoundsEmpty;
    63.     #endregion
    64.     #region ads
    65.     public Camera cam;
    66.     public bool aiming;
    67.     public float hipFov = 75;
    68.     public float aimFov = 55;
    69.     private float curFov = 75;
    70.     public Vector3 hipPos;
    71.     public Vector3 crouchPos;
    72.     public Vector3 aimPos;
    73.     private Vector3 curPos;
    74.     #endregion
    75.     #region recoil
    76.     public Transform camKB;
    77.     public Transform wepKB;
    78.     public float minKB;
    79.     public float maxKB;
    80.     public float minKBSide;
    81.     public float maxKBSide;
    82.     public float returnSpeed = 5f;
    83.     #endregion
    84.     #region muzzle
    85.     public GameObject muzzle;
    86.     #endregion
    87.     #region crosshair
    88.     public float sizeMultiplier = 1f;
    89.     public float aimSpread;
    90.     public float basicSpread = 30;
    91.     public float maximumSpread = 100;
    92.     public float spreadReturnTime = 5;
    93.     public float spreadAddPerShot = 5;
    94.     public float spreadTemp;
    95.     private float spread = 30;
    96.     //Crosshair Textures
    97.     public Texture2D crosshairFirstModeHorizontal;
    98.     public Texture2D crosshairFirstModeVertical;
    99.     #endregion
    100.     #region private
    101.     private Vector2 pivot;
    102.     #endregion
    103.     #region hitMark
    104.     public Texture2D tex;
    105.     public float size = 32;
    106.     private float hitAlpha;
    107.     public AudioClip hitMarkerSound;
    108.     #endregion
    109.     void Start()
    110.     {
    111.         muzzle.SetActive(false);
    112.         if (networkView.isMine)
    113.         {
    114.             spreadTemp = basicSpread;
    115.             spread = basicSpread;
    116.             StartCoroutine(CheckBools());
    117.             StartCoroutine(Draw());
    118.         }
    119.         else
    120.         {
    121.             this.enabled = false;
    122.         }
    123.     }
    124.     void Update()
    125.     {
    126.         if (networkView.isMine)
    127.         {
    128.             if (hitAlpha > 0) hitAlpha -= Time.deltaTime;
    129.             spread = Mathf.Clamp(spread, 0, maximumSpread);
    130.             if (aiming) spread = aimSpread;
    131.             else spread = Mathf.Lerp(spread, spreadTemp + cv.velMag * 2, Time.deltaTime * 8);
    132.             if (spreadTemp > basicSpread) spreadTemp -= Time.deltaTime * spreadReturnTime;
    133.             pivot = new Vector2(Screen.width / 2, Screen.height / 2);
    134.             bulletsLeftRead = bulletsLeft;
    135.             bulletsPerMagRead = bulletsPerMag;
    136.             magsLeftRead = magsLeft;
    137.             camKB.localRotation = Quaternion.Lerp(camKB.localRotation, Quaternion.identity, Time.deltaTime * returnSpeed);
    138.             wepKB.localRotation = Quaternion.Lerp(wepKB.localRotation, Quaternion.identity, Time.deltaTime * returnSpeed);
    139.             cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, curFov, Time.deltaTime * 10);
    140.             transform.localPosition = Vector3.Lerp(transform.localPosition, curPos, Time.deltaTime * 10);
    141.             if (Screen.lockCursor)
    142.                 CheckInput();
    143.             canReloads[1] = true;
    144.             canAims[1] = !cv.running;
    145.             canFires[1] = !cv.running;
    146.             if (aiming)
    147.             {
    148.                 curFov = aimFov;
    149.                 curPos = aimPos;
    150.             }
    151.             else
    152.             {
    153.                 curFov = hipFov;
    154.                 if (cv.state == 0)
    155.                 {
    156.                     curPos = hipPos;
    157.                 }
    158.                 else if (cv.state == 1)
    159.                 {
    160.                     curPos = crouchPos;
    161.                 }
    162.             }
    163.             if (!canAim) aiming = false;
    164.             if (waitingForMouseUp && Input.GetKeyUp(KeyCode.Mouse0))
    165.               waitingForMouseUp = false;
    166.         }
    167.         else
    168.         {
    169.             this.enabled = false;
    170.         }
    171.     }
    172.     void OnGUI()
    173.     {
    174.         if (networkView.isMine)
    175.         {
    176.             float w = crosshairFirstModeHorizontal.width;
    177.             float h = crosshairFirstModeHorizontal.height;
    178.             Rect position1 = new Rect((Screen.width + w) / 2 + (spread * sizeMultiplier), (Screen.height - h) / 2, w, h);
    179.             Rect position2 = new Rect((Screen.width - w) / 2, (Screen.height + h) / 2 + (spread * sizeMultiplier), w, h);
    180.             Rect position3 = new Rect((Screen.width - w) / 2 - (spread * sizeMultiplier) - w, (Screen.height - h) / 2, w, h);
    181.             Rect position4 = new Rect((Screen.width - w) / 2, (Screen.height - h) / 2 - (spread * sizeMultiplier) - h, w, h);
    182.             if (!aiming)
    183.             {
    184.                 GUI.DrawTexture(position1, crosshairFirstModeHorizontal);     //Right
    185.                 GUI.DrawTexture(position2, crosshairFirstModeVertical);     //Up
    186.                 GUI.DrawTexture(position3, crosshairFirstModeHorizontal);     //Left
    187.                 GUI.DrawTexture(position4, crosshairFirstModeVertical);        //Down
    188.             }
    189.             GUI.color = new Color(1, 1, 1, hitAlpha);
    190.             GUI.DrawTexture(new Rect((Screen.width - size) / 2, (Screen.height - size) / 2, size, size), tex);
    191.         }
    192.     }
    193.     void CheckInput()
    194.     {
    195.         aiming = (canAim && Input.GetKey(KeyCode.Mouse1));
    196.         singleShotMode
    197.         if (!reloading && Time.time > timer && canFire && Input.GetKey(KeyCode.Mouse0) && bulletsLeft > 0 && Screen.lockCursor)
    198.         {
    199.           if(singleShotMode && !waitingForMouseUp)
    200.           {
    201.             FireOneShot();
    202.             waitingForMouseUp = true;
    203.           }
    204.         }
    205.         if (!reloading && canReload && magsLeft > 0 && Input.GetKeyDown(KeyCode.R) && Screen.lockCursor)
    206.         {
    207.             reloading = true;
    208.             StartCoroutine(Reload());
    209.         }
    210.        
    211.     }
    212.     void FireOneShot()
    213.     {
    214.         spreadTemp += spreadAddPerShot;
    215.         timer = Time.time + fireRate;
    216.         anim.Rewind(fireAnim.name);
    217.         anim.Play(fireAnim.name);
    218.         localSource.clip = fireSound;
    219.         localSource.PlayOneShot(fireSound);
    220.         StartCoroutine(MuzzleFlash());
    221.         StartCoroutine(Kick3(camKB, new Vector3(-Random.Range(minKB, maxKB), Random.Range(minKBSide, maxKBSide), 0), 0.1f));
    222.         StartCoroutine(Kick3(wepKB, new Vector3(-Random.Range(minKB, maxKB), Random.Range(minKBSide, maxKBSide), 0), 0.1f));
    223.         float actualSpread = Random.Range(-spread, spread);
    224.         //Vector3 position = new Vector3(bulletGo.position.x - actualSpread, bulletGo.position.y - actualSpread, bulletGo.position.z);
    225.         Vector3 direction = gameObject.transform.TransformDirection(new Vector3(Random.Range(-0.01f, 0.01f) * spread, Random.Range(-0.01f, 0.01f) * spread, 1));
    226.         RaycastHit hit2;
    227.         if (Physics.Raycast(bulletGo.position, direction, out hit2, range, hitLayers))
    228.         {
    229.             OnHit(hit2);
    230.         }
    231.         bulletsLeft--;
    232.     }
    233.     void DoHitMark()
    234.     {
    235.         hitAlpha = 2;
    236.         audio.PlayOneShot(hitMarkerSound, 1f);
    237.     }
    238.     void OnHit(RaycastHit hit)
    239.     {
    240.         if (hit.rigidbody)
    241.         {
    242.             hit.rigidbody.AddForceAtPosition(2000 * bulletGo.forward, hit.point);
    243.         }
    244.         if (hit.transform.tag == "Player")
    245.         {
    246.             Instantiate(blood, hit.point, Quaternion.identity);
    247.             DoHitMark();
    248.             if (hit.transform.root.networkView)
    249.                 hit.transform.root.networkView.RPC("ApplyDamage", RPCMode.AllBuffered, Random.Range(damageMin, damageMax), 1);
    250.         }
    251.         else
    252.         {
    253.             if (hit.transform.tag == "Wood")
    254.             {
    255.                 GameObject theObj = Instantiate(wood, hit.point + hit.normal * 0.01f, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
    256.                 theObj.transform.parent = hit.transform;
    257.             }
    258.             else if (hit.transform.tag == "Metal")
    259.             {
    260.                 GameObject theObj = Instantiate(metal, hit.point + hit.normal * 0.01f, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
    261.                 theObj.transform.parent = hit.transform;
    262.             }
    263.             else if (hit.transform.tag == "Dirt")
    264.             {
    265.                 GameObject theObj = Instantiate(dirt, hit.point + hit.normal * 0.01f, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
    266.                 theObj.transform.parent = hit.transform;
    267.             }
    268.             else
    269.             {
    270.                 GameObject theObj = Instantiate(concrete, hit.point + hit.normal * 0.01f, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
    271.                 theObj.transform.parent = hit.transform;
    272.             }
    273.         }
    274.     }
    275.     IEnumerator Kick3(Transform goTransform, Vector3 kbDirection, float time)
    276.     {
    277.         Quaternion startRotation = goTransform.localRotation;
    278.         Quaternion endRotation = goTransform.localRotation * Quaternion.Euler(kbDirection);
    279.         float rate = 1.0f / time;
    280.         var t = 0.0f;
    281.         while (t < 1.0f)
    282.         {
    283.             t += Time.deltaTime * rate;
    284.             goTransform.localRotation = Quaternion.Slerp(startRotation, endRotation, t);
    285.             yield return null;
    286.         }
    287.     }
    288.     IEnumerator Reload()
    289.     {
    290.         reloading = true;
    291.         canAims[0] = false;
    292.         canFires[0] = false;
    293.         canReloads[0] = false;
    294.         if (bulletsLeft > 0)
    295.         {
    296.             StartCoroutine(ReloadingSound(reloadSounds));
    297.             anim.Play(reloadAnim.name);
    298.             yield return new WaitForSeconds(reloadAnim.length);
    299.             bulletsLeft = bulletsPerMag + 1;
    300.             magsLeft--;
    301.         }
    302.         else
    303.         {
    304.             StartCoroutine(ReloadingSound(reloadSoundsEmpty));
    305.             anim.Play(reloadEmptyAnim.name);
    306.             yield return new WaitForSeconds(reloadEmptyAnim.length);
    307.             bulletsLeft = bulletsPerMag;
    308.             magsLeft--;
    309.         }
    310.         canAims[0] = true;
    311.         canFires[0] = true;
    312.         canReloads[0] = true;
    313.         reloading = false;
    314.     }
    315.     IEnumerator ReloadingSound(ReloadSound[] theSound)
    316.     {
    317.         foreach (ReloadSound lol in theSound)
    318.         {
    319.             yield return new WaitForSeconds(lol.length);
    320.             localSource.clip = lol.clip;
    321.             localSource.Play();
    322.         }
    323.     }
    324.     IEnumerator CheckBools()
    325.     {
    326.         CheckAim();
    327.         CheckReload();
    328.         CheckFire();
    329.         yield return new WaitForSeconds(0.1f);
    330.         StartCoroutine(CheckBools());
    331.     }
    332.     IEnumerator MuzzleFlash()
    333.     {
    334.         muzzle.SetActive(true);
    335.         yield return new WaitForSeconds(0.1f);
    336.         muzzle.SetActive(false);
    337.     }
    338.     void CheckAim()
    339.     {
    340.         canAim = false;
    341.         foreach (bool lol in canAims)
    342.         {
    343.             if (!lol) return;
    344.         }
    345.         canAim = true;
    346.     }
    347.     void CheckReload()
    348.     {
    349.         canReload = false;
    350.         foreach (bool lol in canReloads)
    351.         {
    352.             if (!lol) return;
    353.         }
    354.         canReload = true;
    355.     }
    356.     void CheckFire()
    357.     {
    358.         canFire = false;
    359.         foreach (bool lol in canFires)
    360.         {
    361.             if (!lol) return;
    362.         }
    363.         canFire = true;
    364.     }
    365.     IEnumerator Draw()
    366.     {
    367.         canAims[0] = false;
    368.         canFires[0] = false;
    369.         canReloads[0] = false;
    370.         //localSource.clip = drawSound;
    371.         //localSource.Play();
    372.         StartCoroutine(ReloadingSound(drawSound));
    373.         anim.Play(drawAnim.name);
    374.         yield return new WaitForSeconds(drawAnim.length);
    375.         canAims[0] = true;
    376.         canFires[0] = true;
    377.         canReloads[0] = true;
    378.     }
    379. }
     
  3. ggsnip

    ggsnip

    Joined:
    Nov 28, 2014
    Posts:
    3
    okey but i have this error why ?
     

    Attached Files:

  4. Louis-LCM

    Louis-LCM

    Joined:
    Aug 26, 2014
    Posts:
    59
    For the second error insert a "}" at the end of the script .
     
  5. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    Uff sry, just delete the line 196 : singleShotMode

    I usually copy new variables names lower, so i don't forget the names when. Because i'm lazy and i don't like scrolling :rolleyes:

    Edit:

    It wrong also, use this conditions:

    Code (CSharp):
    1. if(singleShotMode)
    2. {
    3.        if(!waitingForMouseUp){
    4.             FireOneShot();
    5.             waitingForMouseUp = true;
    6.       }
    7. }else{
    8.            FireOneShot();
    9. }
     
    Last edited: Nov 28, 2014
  6. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    Ye i realised after second look, if you look my previous post, i edited it a bit
     
  7. ggsnip

    ggsnip

    Joined:
    Nov 28, 2014
    Posts:
    3
    i know XD . Okey the sript work !! thank you :oops: