Search Unity

What's wrong in this code?

Discussion in 'Scripting' started by CheyTac, Jul 26, 2011.

  1. CheyTac

    CheyTac

    Joined:
    Feb 17, 2011
    Posts:
    71
    Hello, I have a Bolt-Action Sniper Rifle and i have this script attached to it:
    Code (csharp):
    1. var isReloading = false;
    2. function Update ()
    3. {
    4.    Screen.lockCursor = true;
    5.    if(Input.GetAxis("Fire1"))
    6.    {
    7.        if(isReloading == false)
    8.      {
    9.        audio.Play();
    10.            Reload();
    11.          }
    12.    }
    13. }
    14.  
    15. function Reload()
    16. {
    17.        animation.Play("reload");
    18.        if(!animation.isPlaying)
    19.          {
    20.             isReloading = true;
    21.          }
    22.        else
    23.          {
    24.             isReloading = false;
    25.      }
    26.  }
    Its supossed to play a gun shot sound when pressing the Left Mouse Button (Fire1), but i dont want to play that sound when playing the "reload" animation, but it doesn't seem to work.

    Can someone please tell me what im doing wrong?

    Thank You! :razz:
     
    Last edited: Jul 26, 2011
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    I'm sure you wanted to use GetButtonDown for fire, get axis is for axises (sticks - mouse movement)

    Also depending on if you want reload to ever get a sound, consider using PlayOneShot so fire can continue to play when you kick of the reload audio :)
     
  3. CheyTac

    CheyTac

    Joined:
    Feb 17, 2011
    Posts:
    71
    oh, so there is why the gunshot sound plays lots of times when clicking once the mouse button... hehe Thank u for that... :D

    here is my code now:
    Code (csharp):
    1. var isReloading = false;
    2. var Clip : AudioClip;
    3. function Update ()
    4. {
    5. Screen.lockCursor = true;
    6. if(Input.GetButtonDown("Fire1"))
    7.   {
    8.      if(isReloading == false)
    9.      {
    10.          audio.PlayOneShot(Clip);
    11.          Reload();
    12.      }
    13.      
    14.    }
    15. }
    16.  
    17. function Reload()
    18. {
    19.         animation.Play("reload");
    20.          if(!animation.isPlaying)
    21.          {
    22.          isReloading = true;
    23.         }
    24.         else
    25.         {
    26.          isReloading = false;
    27.         }
    28.  }
    it still play the gunshot sound when reloading :(
     
  4. CheyTac

    CheyTac

    Joined:
    Feb 17, 2011
    Posts:
    71
    Forget it i have figured it out:

    Code (csharp):
    1. var CanShot = true;
    2. var Shot : AudioClip;
    3. var ReloadS : AudioClip;
    4. function Update ()
    5. {
    6. Screen.lockCursor = true;
    7. if(Input.GetButtonDown("Fire1"))
    8.   {
    9.      if(CanShot)
    10.       {
    11.        if(!animation.isPlaying)
    12.          {
    13.             audio.PlayOneShot(Shot);
    14.             CanShot = false;
    15.           }
    16.        }
    17.    }
    18. if(Input.GetButtonDown("Reload"))
    19.    {
    20.          Reload();
    21.          CanShot = true;
    22.     }
    23. }
    24.  
    25. function Reload()
    26. {
    27.        animation.Play("reload");
    28.         audio.PlayOneShot(ReloadS);
    29. }
    30.