Search Unity

reloading error fps1.27 ETeeski tutorial

Discussion in 'Scripting' started by omgitskara, Feb 7, 2013.

  1. omgitskara

    omgitskara

    Joined:
    Jan 26, 2013
    Posts:
    9
    Link to video - http://www.youtube.com/watch?v=G-EhZAJUdg0

    Hi, I followed this video thru but I'm experiencing a bug. When I hold down Fire1 til the clip = 0 the gun stops but when I click Fire1 again, It shoots a bullet and at the same time reloads. I think this bug is the reason for another bug which is being able to reload while aiming. Any ideas on how to fix this would be greatly appreciated.

    Also a random question for ETeeski. I noticed the guitar icon on your screen in your tutorials, is that Rocksmith? That game is awesome. =]

    GunScript code:
    Code (csharp):
    1. var beingHeld : boolean = false;
    2. var outsideBox : GameObject;
    3. var countToThrow : int = -1;
    4. var playerTransform : Transform;
    5. var playerMovementScript : PlayerMovementScript;
    6. var cameraObject : GameObject;
    7. var targetXRotation : float;
    8. var targetYRotation : float;
    9. var targetXRotationV : float;
    10. var targetYRotationV : float;
    11. var rotateSpeed : float = 0.3;
    12. var holdHeight : float = -0.5;
    13. var holdSide : float = 0.5;
    14. var ratioHipHold : float = 1;
    15. var hipToAimSpeed : float = 0.1;
    16. var ratioHipHoldV : float;
    17. var aimRatio : float = 0.4;
    18. var zoomAngle : float = 30;
    19. var fireSpeed : float = 15;
    20. var waitTilNextFire : float = 0;
    21. var bullet : GameObject;
    22. var bulletSpawn : GameObject;
    23. var shootAngleRandonizationAiming : float = 5;
    24. var shootAngleRandomizationNotAiming : float = 15;
    25. var recoilAmount : float = 0.5;
    26. var recoilRecoverTime : float = 0.2;
    27. var currentRecoilZPos : float;
    28. var currentRecoilZPosV : float;
    29. var bulletSound : GameObject;
    30. var muzzleFlash : GameObject;
    31. var gunbobAmountX : float = 0.5;
    32. var gunbobAmountY : float = 0.5;
    33. var currentGunbobX : float;
    34. var currentGunbobY : float;
    35. var gunModelObjects : GameObject[];
    36. var reloading : boolean = false;
    37. var reloadAnimation : Animation;
    38. var reloadSound : AudioSource;
    39. var reloadAnimationString : String;
    40. var clipSize : int = 25;
    41. var currentClip : int = 25;
    42. var maxExtraAmmo : int = 100;
    43. var currentExtraAmmo : int = 100;
    44.  
    45. function Awake ()
    46. {
    47.     countToThrow = -1;
    48.     playerTransform = GameObject.FindWithTag("Player").transform;
    49.     playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
    50.     cameraObject = GameObject.FindWithTag("MainCamera");
    51.      
    52. }
    53.  
    54. function LateUpdate ()
    55. {
    56. if ( currentClip > clipSize)
    57.     currentClip = clipSize;
    58. if ( currentExtraAmmo > maxExtraAmmo)
    59.     currentExtraAmmo = maxExtraAmmo;
    60. if ( currentClip < 0)
    61.     currentClip = 0;
    62. if ( currentExtraAmmo < 0)
    63.     currentExtraAmmo = 0;  
    64. if (beingHeld)
    65. {
    66.     if (!reloading  Input.GetButtonDown("Reload")  currentClip < clipSize  currentExtraAmmo > 0)
    67.    
    68.         {
    69.             reloading = true;
    70.             reloadAnimation.Play( reloadAnimationString);
    71.             reloadSound.Play();
    72.         }
    73.     if (!reloading  Input.GetButtonDown("Fire1")  currentClip == 0  currentExtraAmmo > 0)
    74.    
    75.         {
    76.             reloading = true;
    77.             reloadAnimation.Play( reloadAnimationString);
    78.             reloadSound.Play();
    79.         }
    80.     if (reloading  reloadAnimation.IsPlaying( reloadAnimationString))
    81.         {
    82.             if ( currentExtraAmmo >= clipSize - currentClip)
    83.              {
    84.                 currentExtraAmmo -= clipSize - currentClip;
    85.                 currentClip = clipSize;
    86.              }
    87.             if ( currentExtraAmmo < clipSize - currentClip)
    88.              {
    89.                 currentClip += currentExtraAmmo;
    90.                 currentExtraAmmo = 0;
    91.              }
    92.             reloading = false;
    93.         }
    94.    
    95.     for(var modelObject : GameObject in gunModelObjects)
    96.     {
    97.         modelObject.layer = 8;
    98.     }
    99.     rigidbody.useGravity = false;
    100.     outsideBox.GetComponent(Collider).enabled = false;
    101.    
    102.     currentGunbobX = Mathf.Sin( cameraObject.GetComponent( LookMouseScript).headbobStepCounter) * gunbobAmountX * ratioHipHold;
    103.     currentGunbobY = Mathf.Cos( cameraObject.GetComponent( LookMouseScript).headbobStepCounter * 2) * gunbobAmountY * -1 * ratioHipHold;
    104.     var holdMuzzleFlash : GameObject;
    105.     var holdSound : GameObject;
    106.     if (Input.GetButton("Fire1")  currentClip > 0  !reloading)
    107.     {
    108.         if (waitTilNextFire <= 0)
    109.         {
    110.                currentClip -= 1;
    111.             if (bullet)
    112.                 Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    113.             if (bulletSound)
    114.                 holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    115.             if (muzzleFlash)
    116.                 holdMuzzleFlash = Instantiate(muzzleFlash, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    117.                    
    118.                 targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandonizationAiming, shootAngleRandomizationNotAiming, ratioHipHold);
    119.                 targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandonizationAiming, shootAngleRandomizationNotAiming, ratioHipHold);
    120.                 currentRecoilZPos -= recoilAmount;
    121.                
    122.                 waitTilNextFire = 1;
    123.         }
    124.     }
    125.     waitTilNextFire -= Time.deltaTime * fireSpeed;
    126.     if (holdSound)
    127.         holdSound.transform.parent = transform;
    128.     if (holdMuzzleFlash)
    129.         holdMuzzleFlash.transform.parent = transform;  
    130.        
    131.     currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
    132.     cameraObject.GetComponent(LookMouseScript).currentTargetCameraAngle = zoomAngle;
    133.    
    134.     if (Input.GetButton("Fire2")  !reloading){
    135.         cameraObject.GetComponent(LookMouseScript).currentAimRatio = aimRatio;
    136.         ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 0, ratioHipHoldV, hipToAimSpeed);}
    137.    
    138.     if (Input.GetButton("Fire2") == false || reloading){
    139.         cameraObject.GetComponent(LookMouseScript).currentAimRatio = 1;
    140.         ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 1, ratioHipHoldV, hipToAimSpeed);}
    141.        
    142.     transform.position = cameraObject.transform.position + (Quaternion.Euler(0, targetYRotation, 0) * Vector3(holdSide * ratioHipHold + currentGunbobX, holdHeight * ratioHipHold + currentGunbobY, 0) + Quaternion.Euler( targetXRotation, targetYRotation, 0) * Vector3(0, 0, currentRecoilZPos));
    143.     targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(LookMouseScript).xRotation, targetXRotationV, rotateSpeed);
    144.     targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(LookMouseScript).yRotation, targetYRotationV, rotateSpeed);
    145.    
    146.     transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
    147. }
    148. if (!beingHeld)
    149.  
    150. {
    151.     for(var modelObject : GameObject in gunModelObjects)
    152.     {
    153.         modelObject.layer = 0;
    154.     }
    155.     rigidbody.useGravity = true;
    156.  
    157.     outsideBox.GetComponent(Collider).enabled = true;
    158.  
    159.    
    160.  
    161.     countToThrow -= 1;
    162.  
    163.     if (countToThrow == 0)
    164.  
    165.         rigidbody.AddRelativeForce(0, playerMovementScript.throwGunUpForce, playerMovementScript.throwGunForwardForce);
    166.  
    167.    
    168.  
    169.     if (Vector3.Distance(transform.position, playerTransform.position) < playerMovementScript.distToPickUpGun  Input.GetButtonDown("Use Key")  playerMovementScript.waitFrameForSwitchGuns <= 0)
    170.  
    171.     {
    172.  
    173.         playerMovementScript.currentGun.GetComponent(GunScript).beingHeld = false;
    174.  
    175.         playerMovementScript.currentGun.GetComponent(GunScript).countToThrow = 2;
    176.  
    177.         playerMovementScript.currentGun = gameObject;
    178.  
    179.         beingHeld = true;
    180.  
    181.         targetYRotation = cameraObject.GetComponent(LookMouseScript).yRotation - 180;
    182.  
    183.         playerMovementScript.waitFrameForSwitchGuns = 2;
    184.  
    185.     }
    186.  
    187. }
    188.  
    189. }
     
  2. SanthanaBharathy

    SanthanaBharathy

    Joined:
    Oct 6, 2012
    Posts:
    77
  3. omgitskara

    omgitskara

    Joined:
    Jan 26, 2013
    Posts:
    9
    Thanks for taking the time to look over my code. Did you mean to change line 11 to that? I did and my game plays exactly the same as before.
     
  4. FirePlantGames

    FirePlantGames

    Joined:
    Dec 11, 2012
    Posts:
    49
    well personally I like having

    if (!reloading Input.GetButton("Fire1") currentClip == 0 currentExtraAmmo > 0)
    instead of
    if (!reloading Input.GetButtonDown("Fire1") currentClip == 0 currentExtraAmmo > 0)
    (at line 73)

    this way you won't have to press the fire button again to reload it's automatic

    as for your problem at line 80 you have: if (reloading reloadAnimation.IsPlaying( reloadAnimationString))

    it should be if (reloading !reloadAnimation.IsPlaying( reloadAnimationString))


    i think anyways get back to me and tell me if it works