Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Freeze on button press

Discussion in 'Scripting' started by GForce007, Oct 20, 2015.

  1. GForce007

    GForce007

    Joined:
    May 22, 2015
    Posts:
    27
    Hi all!!

    I hope this is the right place to post this specific question.

    I have a gun controller script attached to my player object for my FPS game.
    The gun controller handles the gun swaps and gun creation from prefabs.

    My problem is, when I press a number key to change weapons, the mouse look stops working till the gun is created. Everything else works fine while the gun is created, but not the mouse look. Also when I jump and try to shoot while the player is in the air, it does not work, hold in the shoot button and then jump its fine.

    I'm using the FPS controller that comes with Unity's assets.

    Here is the code, not polished and in a completed form as I'd like it but here it is:
    Code (csharp):
    1.  
    2.  
    3. void FixedUpdate ()
    4.     {
    5.         ammoText.text = currentGunAmmo.ToString(); // Update Ammo text
    6.  
    7.         GunSwap(gunSwap); // call this function which calls the gun create function
    8.  
    9.         if(currentGunAmmo == 0) // check if ammo is 0 and all weapons are not empty as well
    10.         {
    11.             gunsAmmo[currentWeapon] = currentGunAmmo;
    12.             GunEmpty(); //  function that switches to new gun with ammo
    13.         }
    14.  
    15.         if(Input.GetKey("1") && currentWeapon != 0 && gunsAmmo[0] != 0 && canSwap == true) // check if weapon's done shooting
    16.         {                                                                                  // before swapping over
    17.             gunsAmmo[currentWeapon] = currentGunAmmo;
    18.           currentWeapon = 0;
    19.            gunSwap = true;
    20.         }
    21.      
    22.         else if(Input.GetKey("2") && currentWeapon != 1 && gunsAmmo[1] != 0 && canSwap == true) // check if weapon's done shooting
    23.         {                                                                                       // before swapping over
    24.             gunsAmmo[currentWeapon] = currentGunAmmo;
    25.             currentWeapon = 1;
    26.             gunSwap = true;
    27.         }
    28.  
    29.         else if(Input.GetKey("3") && currentWeapon != 2 && gunsAmmo[2] != 0 && canSwap == true) // check if weapon's done shooting
    30.         {                                                                                       // before swapping over
    31.             gunsAmmo[currentWeapon] = currentGunAmmo;
    32.             currentWeapon = 2;
    33.             gunSwap = true;
    34.         }
    35.  
    36.         else if(Input.GetKey("4") && currentWeapon != 3 && gunsAmmo[3] != 0 && canSwap == true) // check if weapon's done shooting
    37.         {                                                                                       // before swapping over
    38.             gunsAmmo[currentWeapon] = currentGunAmmo;
    39.             currentWeapon = 3;
    40.             gunSwap = true;
    41.         }
    42.  
    43.         else if(Input.GetKey("5") && currentWeapon != 4 && gunsAmmo[4] != 0 && canSwap == true) // check if weapon's done shooting
    44.         {                                                                                       // before swapping over
    45.             gunsAmmo[currentWeapon] = currentGunAmmo;
    46.             currentWeapon = 4;
    47.             gunSwap = true;
    48.         }
    49.  
    50.         else if(Input.GetKey("6") && currentWeapon != 5 && gunsAmmo[5] != 0 && canSwap == true) // check if weapon's done shooting
    51.         {                                                                                       // before swapping over
    52.             gunsAmmo[currentWeapon] = currentGunAmmo;
    53.             currentWeapon = 5;
    54.             gunSwap = true;
    55.         }
    56.  
    57.  
    58.  
    59.         GunBob(); // Gunsway function
    60.     }
    61.  
    62.  
    Thanx!!!
     
  2. GForce007

    GForce007

    Joined:
    May 22, 2015
    Posts:
    27
    Just a side note what I've picked up just now, it actually does not matter what button I press, the mouse look locks up.
    Is it maybe the FPS controller, anyone else had or have this problem?

    Thanx.