Search Unity

ETeeskiTutorials FPS1.12 Problem

Discussion in 'Scripting' started by clownwiz1, Jul 20, 2012.

  1. clownwiz1

    clownwiz1

    Joined:
    Jul 20, 2012
    Posts:
    2
    I have problem with FPS1.18 muzzle flash

    The gun looks fine in scene view but when I test the game out the gun goes way off to the side
    When I exit out of play mode the gun goes back to the original location.
    It only started doing this with this tutorial.
    Here is my gun script.

    Code (csharp):
    1. var cameraObject : GameObject;
    2. @HideInInspector
    3. var targetXRotation : float;
    4. @HideInInspector
    5. var targetYRotation : float;
    6. @HideInInspector
    7. var targetXRotationV : float;
    8. @HideInInspector
    9. var targetYRotationV : float;
    10.  
    11. var rotateSpeed : float = 0.3;
    12.  
    13. var holdHeight : float = -0.5;
    14. var holdSide : float = 0.5;
    15. var ratioHipHold : float = 1;
    16. var hipToAimSpeed : float = 0.1;
    17. @HideInInspector
    18. var ratioHipHoldV : float;
    19.  
    20. var aimRatio : float = 0.4;
    21.  
    22. var zoomAngle : float = 30;
    23.  
    24. var fireSpeed : float = 15;
    25. @HideInInspector
    26. var waitTillNextFire : float = 0;
    27. var bullet : GameObject;
    28. var bulletSpawn : GameObject;
    29.  
    30. var shootAngleRandomizationAiming : float = 5;
    31. var shootAngleRandomizationNotAiming : float = 15;
    32.  
    33. var recoilAmount : float = 0.5;
    34. var recoilRecoverTime : float = 0.2;
    35. @HideInInspector
    36. var currentRecoilZPos : float;
    37. @HideInInspector
    38. var currentRecoilZposV : float;
    39.  
    40. var bulletSound : GameObject;
    41. var muzzleFlash : GameObject;
    42.  
    43. function Update ()
    44. {
    45.     var holdMuzzleFlash : GameObject;
    46.    
    47.     var holdSound : GameObject;
    48.    
    49.     if(Input.GetButton("Fire1"))
    50.     {
    51.         if(waitTillNextFire <= 0)
    52.         {
    53.             if (bullet)
    54.                 Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    55.             if (bulletSound)
    56.                 holdSound = Instantiate(bulletSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    57.             if (muzzleFlash)
    58.                 holdMuzzleFlash = Instantiate(muzzleFlash, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    59.             targetXRotation += ( Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming,ratioHipHold);
    60.             targetYRotation += ( Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming,ratioHipHold);
    61.             currentRecoilZPos -= recoilAmount;
    62.             waitTillNextFire = 1;
    63.         }
    64.     }
    65.     waitTillNextFire -= Time.deltaTime * fireSpeed;
    66.    
    67.     if (holdSound)
    68.         holdSound.transform.parent = transform;
    69.     if (holdMuzzleFlash)
    70.         holdMuzzleFlash.transform.parent = transform;
    71.    
    72.     currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZposV, recoilRecoverTime);
    73.    
    74.     cameraObject.GetComponent(MouseLookScript).currentTargetCameraAngle = zoomAngle;
    75.  
    76.     if(Input.GetButton("Fire2")){
    77.         cameraObject.GetComponent(MouseLookScript).currentAimRatio = aimRatio;
    78.         ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 0, ratioHipHoldV, hipToAimSpeed);}
    79.     if(Input.GetButton("Fire2") == false){
    80.         cameraObject.GetComponent(MouseLookScript).currentAimRatio = 1;
    81.         ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 1, ratioHipHoldV, hipToAimSpeed);}
    82.        
    83.     transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * ratioHipHold, holdHeight * ratioHipHold, 0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0,0,currentRecoilZPos));
    84.  
    85.     targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
    86.     targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);
    87.    
    88.     transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
    89. }
    Thanks for the help.
     
    Last edited: Aug 2, 2012