Need a bit of help with my script (about 100 lines)

The below is my code, I have a few problems with it:

First off, When I left click, no sound happens (yes sound files are attached to the value) And I would like to know how to make the ray shoot in the instance of me left clicking and not having it be as long as I hold down the trigger

Secondly, the reload sound happens every two seconds, when It should happen every 0.7 seconds.

Thirdly, I don’t know how but I would like the reloading sequence to repeat it’s self until it is false so that if I have say three rounds left inside the mag, it goes *Click *Click and stop there since the value of the ammo is equal to 5.

Lastly, I have no Idea of how to have a cool down time between every shot. So any help would be welcome. I have the var already

If you happen to find other issues with my code please speak of it, Thanks

*BTW I have a ray origin and a Sound origin allready

   var Range : float = 250;
   
   var Force : float = 1000;

   var Ammo : int = 60;
   
   var BulletsPerClip : int = 5;
   
   var ReloadTime : float = 0.7;
   
   var BulletsLeft : int = 0;
   
   var ShootTimer : float = 0;
   
   var ShootCooler : float = 1;
   
   public var ShootAudio : AudioClip;
   
   public var ReloadAudio : AudioClip;
   
   public var ArmAudio : AudioClip;


function Start (){
   BulletsLeft = BulletsPerClip;
}

function Update () {
   if(Input.GetKey( KeyCode.Mouse0 )){
       RayShoot ();
   }
}
   
     
          
               
                    
function RayShoot () {
   var Hit : RaycastHit;
   var DirectionRay = transform.TransformDirection( Vector3.forward );
   Debug.DrawRay( transform.position , DirectionRay * Range , Color.red );
            
   if ( BulletsLeft < 0 )
           
      if(Physics.Raycast( transform.position , DirectionRay , Hit , Range ));
            
            if( Hit.rigidbody ){
            
            Hit.rigidbody.AddForceAtPosition( DirectionRay * Force , Hit.point );
            
            audio. PlayOneShot (ShootAudio);
            
            }
            
            }
            
            BulletsLeft --;
            
            if( BulletsLeft < 0){
                BulletsLeft = 0;
            if( BulletsLeft ==0 ){
                Reload ();
            }
}
            
function Reload (){
   if( Input.GetKey( KeyCode.R ));
   yield WaitForSeconds( ReloadTime );
   if( BulletsLeft < BulletsPerClip );
       BulletsLeft ++ ;
   audio. PlayOneShot (ReloadAudio);
   yield WaitForSeconds( ReloadTime );
   if( BulletsLeft < BulletsPerClip );
       BulletsLeft ++ ;
   audio. PlayOneShot (ReloadAudio); 
   yield WaitForSeconds( ReloadTime );
   if( BulletsLeft < BulletsPerClip );
       BulletsLeft ++ ;
   audio. PlayOneShot (ReloadAudio);
   yield WaitForSeconds( ReloadTime );
   if( BulletsLeft < BulletsPerClip );
       BulletsLeft ++ ;
   audio. PlayOneShot (ReloadAudio);
   yield WaitForSeconds( ReloadTime );
   if( BulletsLeft < BulletsPerClip );
       BulletsLeft ++ ;
   audio. PlayOneShot (ReloadAudio);
   yield WaitForSeconds( ReloadTime );
   if( BulletsLeft < BulletsPerClip );
       BulletsLeft ++ ;
   audio. PlayOneShot (ReloadAudio);
   audio. PlayOneShot (ArmAudio);
}

Well, there are multiple issues here, so let me deal with them one by one from the top.

ONE: if you use

if(Input.GetKeyDown( KeyCode.Mouse0 )){

instead of

if(Input.GetKey( KeyCode.Mouse0 )){

it’ll only trigger when the player first puts their finger on the button.

TWO: Inside your ‘RayHit’ function, you have an if statement which doesn’t end in an open-close curly bracket, it instead ends in a semicolon. While this is syntactically correct, it is unlikely to do what you want it to (because there’s no point having an if statement without some code for it to trigger). Get rid of the semicolon at the end of

if(Physics.Raycast( transform.position , DirectionRay , Hit , Range ));

(so)

if(Physics.Raycast( transform.position , DirectionRay , Hit , Range )){
    // Do some hit mechanics here
}

THREE: I would put the bullet depletion and reloading stuff outside of the RaycastHit if statemend discussed above, because otherwise you don’t lose any ammo from shooting into the sky. Also play the audio at that point.

FOUR: What’s the ‘if( Input.GetKey( KeyCode.R ));’ doing at the top of your reload function? Not only is it another non-functional if statement, in this case I don’t think you want it at all! Get rid of that line, and replace it with something in your Update Loop which allows you to manually start reloading.

(reloading is a boolean value defined at the top- you’ll have to add it. It stops the player from shooting or attempting to reload while the gun is still being loaded. You may want to have some kind of override for this, if it’s a bolt-action weapon and you want to be able to interrupt the reload action, at the cost of having a smaller clip)

function Update () {
    if(reloading)
    {
        if(Input.GetKeyDown( KeyCode.Mouse0 )){
            RayShoot ();
        }
        if(Input.GetKeyDown(KeyCode.R))
        {
            Reload();
        }
    }
}

FIVE: Your reload function has a lot of reused code, and could be simplified a lot. At the moment, it will always try to reload exactly 6 bullets, and doesn’t check for if the clip is full or not before going on to the next reload step. It’d be better to do something like

while(bulletsLeft < bulletsPerClip)
{
    audio.PlayOneShot (ReloadAudio);
    yield WaitForSeconds( ReloadTime );
    bulletsLeft++;
}
audio.PlayOneShot (ArmAudio);

SIX: Please, make all of your member names lower-case. It seems like nit-picking, but the time will come when you can’t tell the difference between a type and a member, and that’ll cost you.