Search Unity

[FPS] Chapter 1.23 It Wont Swich Guns

Discussion in 'Scripting' started by Squall Leonhart, Mar 17, 2012.

  1. Squall Leonhart

    Squall Leonhart

    Joined:
    Mar 17, 2012
    Posts:
    4
    Hey Im watching Eteeski (And you might know me as Angel4653 from youtube)

    I press the action button and it wont do the action I want it to take :(
    this the code it has that makes the action run please help


    Gun Script
    Code (csharp):
    1. var beingHeld : boolean = false;
    2. var outsideBox : GameObject;
    3. @HideInInspector
    4. var countToThrow: int = -1;
    5. @HideInInspector
    6. var playerTransform : Transform;
    7. @HideInInspector
    8. var playerMovementScript: MovementScript;
    9. @HideInInspector
    10. var cameraObject : GameObject;
    11. @HideInInspector
    12. var targetXRotation : float;
    13. @HideInInspector
    14. var targetYRotation : float;
    15. @HideInInspector
    16. var targetXRotationV : float;
    17. @HideInInspector
    18. var targetYRotationV : float;
    19.  
    20. var rotateSpeed : float = 0.3;
    21.  
    22. var holdHeight : float = -0.5;
    23. var holdSide : float = 0.5;
    24. var racioHipHold : float = 1;
    25. var hipToAimSpeed : float = 0.1;
    26. @HideInInspector
    27. var racioHipHoldV : float;
    28.  
    29. var aimRacio : float = 0.4;
    30.  
    31. var zoomAngle : float = 30;
    32.  
    33. var fireSpeed : float = 15;
    34. @HideInInspector
    35. var waitTilNextFire : float = 0;
    36. var bullet : GameObject;
    37. var bulletSpawn : GameObject;
    38. var shootAngleRandomizationAiming : float = 5;
    39. var shootAngleRandomizationNotAiming : float = 15;
    40.  
    41. var recoilAmount : float = 0.5;
    42. var recoilRecoverTime : float = 0.2;
    43. @HideInInspector
    44. var currentRecoilZPos : float;
    45. @HideInInspector
    46. var currentRecoilZPosV : float;
    47.  
    48. var bulletSound : GameObject;
    49. var muzzelFlash : GameObject;
    50.  
    51. var gunBobAmountX : float = 0.5;
    52. var gunBobAmountY : float = 0.2;
    53. @HideInInspector
    54. var currentGunBobX : float;
    55. @HideInInspector
    56. var currentGunBobY : float;
    57.  
    58. var damage : float = 15;
    59.  
    60. function Awake ()
    61. {
    62.     countToThrow = -1;
    63.     playerTransform = GameObject.FindWithTag("Player").transform;
    64.     playerMovementScript = GameObject.FindWithTag("Player").GetComponent(MovementScript);
    65.     cameraObject = GameObject.FindWithTag("MainCamera");
    66.    
    67. }
    68.  
    69. function LateUpdate ()
    70. {
    71. if (beingHeld)
    72. {  
    73.     rigidbody.useGravity = false;
    74.     outsideBox.GetComponent(Collider).enabled = false;
    75.    
    76.     var holdSound : GameObject;
    77.     var holdFlash : GameObject;
    78.    
    79.  
    80.     if (Input.GetButton("Fire1"))
    81.     {
    82.         if (waitTilNextFire <= 0)
    83.         {
    84.             if (bullet)
    85.                 Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.transform.rotation);
    86.            
    87.             if (bulletSound)
    88.                 holdSound = Instantiate(bulletSound, bulletSpawn.transform.position,bulletSound.transform.rotation);
    89.                
    90.             if (muzzelFlash)
    91.                 holdFlash = Instantiate(muzzelFlash, bulletSpawn.transform.position,bulletSpawn.transform.rotation);
    92.             targetXRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    93.             targetYRotation += (Random.value - 0.5) * Mathf.Lerp(shootAngleRandomizationAiming, shootAngleRandomizationNotAiming, racioHipHold);
    94.             currentRecoilZPos -= recoilAmount;
    95.             waitTilNextFire = 1;
    96.         }
    97.     }
    98.     waitTilNextFire -= Time.deltaTime * fireSpeed;
    99.     if(holdSound)
    100.     holdSound.transform.parent = transform;
    101.     if(holdFlash)
    102.     holdFlash.transform.parent = transform;
    103.    
    104.     currentRecoilZPos = Mathf.SmoothDamp( currentRecoilZPos, 0, currentRecoilZPosV, recoilRecoverTime);
    105.  
    106.     cameraObject.GetComponent(MouseLookScript).currentTargetCameraAngle = zoomAngle;
    107.  
    108.     if (Input.GetButton("Fire2"))
    109.     {
    110.         cameraObject.GetComponent(MouseLookScript).currentAimRacio = aimRacio;
    111.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 0, racioHipHoldV, hipToAimSpeed);}
    112.     if (Input.GetButton("Fire2") == false)
    113.     {
    114.         cameraObject.GetComponent(MouseLookScript).currentAimRacio = 1;
    115.         racioHipHold = Mathf.SmoothDamp(racioHipHold, 1, racioHipHoldV, hipToAimSpeed);}
    116.  
    117.     transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * racioHipHold, holdHeight * racioHipHold, 0) + Quaternion.Euler(targetXRotation, targetYRotation, 0) * Vector3(0,0,currentRecoilZPos));
    118.    
    119.     targetXRotation = Mathf.SmoothDamp( targetXRotation, cameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
    120.     targetYRotation = Mathf.SmoothDamp( targetYRotation, cameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);
    121.    
    122.     transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
    123.    
    124.     currentGunBobX = Mathf.Sin(cameraObject.GetComponent(MouseLookScript).headStepCounter) * gunBobAmountX * racioHipHold;
    125.     currentGunBobY = Mathf.Cos(cameraObject.GetComponent(MouseLookScript).headStepCounter * 2) * gunBobAmountX * racioHipHold;
    126.    
    127.     transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * racioHipHold + currentGunBobX, holdHeight * racioHipHold + currentGunBobY,0) + Quaternion.Euler(targetXRotation,targetYRotation,0) * Vector3(0,0,currentRecoilZPos));
    128.     }
    129.  
    130. if (!beingHeld)
    131. {
    132.     rigidbody.useGravity = true;
    133.     outsideBox.GetComponent(Collider).enabled = true;
    134.    
    135.     countToThrow -= 1;
    136.     if (countToThrow == 0)
    137.        // rigidbody.AddRelativeForce(0, playerMovementScript.throwGunUpForce, playerMovementScript.throwGunForwardForce);
    138.         rigidbody.AddRelativeForce(0,100,300);
    139.        
    140.     if (Vector3.Distance(transform.position, playerTransform.position) < playerMovementScript.distToPickUpGun  Input.GetButtonDown("Equip")  playerMovementScript.waitFrameForSwitchGuns <= 0 )
    141.         {
    142.         playerMovementScript.currentGun.GetComponent(GunScript).beingHeld = false;
    143.         playerMovementScript.currentGun.GetComponent(GunScript).countToThrow = 2;
    144.         playerMovementScript.currentGun = gameObject;
    145.         beingHeld = true;
    146.         targetYRotation = cameraObject.GetComponent(MouseLookScript).yRotation - 180;
    147.         playerMovementScript.waitFrameForSwitchGuns = 2;
    148.     }
    149. }
    150. }
    151.  
    PlayerMovementScript
    Code (csharp):
    1.  
    2. var walkAccelaration: float = 5;
    3. var walkDeacceleration: float = 5;
    4. @HideInInspector
    5. var walkDeaccelerationx: float;
    6. @HideInInspector
    7. var walkDeaccelerationz: float;
    8. @HideInInspector
    9. var horizontalMovement : Vector2;
    10. var cameraObject : GameObject;
    11. var crouchDetectionObject : GameObject;
    12. var maxWalkSpeed : float = 20;
    13.  
    14. var maxSlope : float = 60;
    15. var grounded : boolean = true;
    16.  
    17. var crouchRatio : float = 0.5;
    18. var transitionToCrouchSec : float = 0.4;
    19. var crouchVelocity : float;
    20. var currentRatioCrouch : float = 1;
    21. var originalLocalScaleY : float;
    22. var crouchLocalScaleY : float;
    23.  
    24. var currentGun : GameObject;
    25. var distToPickUpGun : float = 10;
    26. var throwGunUpForce : float = 100;
    27. var throwGunFowardForce : float = 300;
    28.  
    29. var waitFrameForSwitchGun : int = -1;
    30.  
    31. function Awake ()
    32. {
    33.     currentRatioCrouch = 1;
    34.     originalLocalScaleY = transform.localScale.y;
    35.     crouchLocalScaleY = transform.localScale.y * crouchRatio;
    36. }
    37. function Update ()
    38. {
    39.     waitFrameForSwitchGun -= 1;
    40.    
    41.     horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
    42.     if (horizontalMovement.magnitude > maxWalkSpeed)
    43.     {
    44.         horizontalMovement = horizontalMovement.normalized;
    45.         horizontalMovement *= maxWalkSpeed;
    46.        
    47.     }
    48.     rigidbody.velocity.x = horizontalMovement.x;
    49.     rigidbody.velocity.z = horizontalMovement.y;
    50.    
    51.     if (Input.GetAxis("Horizontal") == 0  Input.GetAxis("Vertical") == 0  grounded){
    52.         rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationx,walkDeacceleration);
    53.         rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationz,walkDeacceleration);}
    54.        
    55.     transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
    56.     rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAccelaration * Time.deltaTime,0, Input.GetAxis("Vertical") * walkAccelaration* Time.deltaTime);
    57.    
    58.     transform.localScale.y = Mathf.Lerp(crouchLocalScaleY, originalLocalScaleY, currentRatioCrouch);
    59.     if(Input.GetButton("Crouch"))
    60.         currentRatioCrouch = Mathf.SmoothDamp(currentRatioCrouch,0,crouchVelocity,transitionToCrouchSec);
    61.     if(Input.GetButton("Crouch") == false  crouchDetectionObject.GetComponent(CollisionDetectionScript).collisionDetected == false)
    62.         currentRatioCrouch = Mathf.SmoothDamp(currentRatioCrouch,1,crouchVelocity,transitionToCrouchSec);
    63.        
    64.    
    65.    
    66. }
     
    Last edited: Mar 18, 2012
  2. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    try putting a Debug.Log function in the part that picks up the gun to make sure that if statement is being triggered.

    Code (csharp):
    1.  
    2. if (Vector3.Distance(transform.position, playerTransform.position) < playerMovementScript.distToPickUpGun  Input.GetButtonDown("Equip")  playerMovementScript.waitFrameForSwitchGuns <= 0 )
    3.  
    4.         {
    5.         Debug.Log("Switch Guns");
    6.  
    7.         playerMovementScript.currentGun.GetComponent(GunScript).beingHeld = false;
    8.  
    9.         playerMovementScript.currentGun.GetComponent(GunScript).countToThrow = 2;
    10.  
    11.         playerMovementScript.currentGun = gameObject;
    12.  
    13.         beingHeld = true;
    14.  
    15.         targetYRotation = cameraObject.GetComponent(MouseLookScript).yRotation - 180;
    16.  
    17.         playerMovementScript.waitFrameForSwitchGuns = 2;
    18.  
    19.     }
    20.  
    Try putting the Debug.Log function in different places to see if there are any parts of code that are neven even run.
     
  3. Squall Leonhart

    Squall Leonhart

    Joined:
    Mar 17, 2012
    Posts:
    4
    I just found the problem and now it changes and works properly
     
  4. UltimateGameCore

    UltimateGameCore

    Joined:
    Jul 3, 2013
    Posts:
    3
    What was the problem. I have the same problem.