Search Unity

Resolved Problem with Player Rotation when switching Weapon from Primary to Secondary

Discussion in 'Scripting' started by KaiXGT, Apr 18, 2021.

  1. KaiXGT

    KaiXGT

    Joined:
    Feb 12, 2021
    Posts:
    13
    I'm currently working on a common FPS shooter mechanic to swap weapons from the primary weapon to a secondary. The method I use in getting this is simply by activating the weapon game object and disabling the other. When testing this out, I realize that when a weapon is swapped, the rotation of the player game object changes along the y-axis. Something else that I also noticed that I think would be useful to know is that the y-axis resets to the value that it had when using another weapon.

    In case you don't get it, here's a simpler explanation:

    Player is using Weapon A, rotates so that the y-axis becomes 68.23
    Player switches to Weapon B, y-axis resets to 0 (default rotation set in editor)
    Player rotates so that the y-axis becomes 24.87 while using Weapon B
    Players switches to Weapon A, y-axis resets to 68.23
    Player switches to Weapon B, y-axis resets to 24.87

    Now the way I tried to fix this was to get the players rotation stored in a quaternion before they swap their weapon, and when the boolean that is set to true when the player swaps their weapon is true the player's rotation on the y-axis would be set to the rotation on the y-axis stored in the quaternion and then the boolean would be set to false.

    Half of this works. When the player swaps their weapon, the correct rotation is stored into the quaternion, but it does not set the player's y-axis value to the quaternions y-axis value.

    My code looks something like this:

    Set Player Rotation on Y-Axis:

    Code (CSharp):
    1.         if (swappedWeapon)
    2.         {
    3.             Quaternion playerRotation; // create new quaternion to modify y-axis value
    4.  
    5.             playerRotation = player.transform.rotation; // set playerRotation quaternion to players rotation
    6.             playerRotation.y = rotation.y; // set playerRotation y-axis value to rotation (pre-existing quaternion that takes players initial rotation) y-axis value
    7.  
    8.             StartCoroutine(SwappedWeaponFalse());
    9.         }
    10.  
    11.         IEnumerator SwappedWeaponFalse()
    12.         {
    13.             yield return new WaitForSeconds(0.05f); // wait 0.05 seconds before switching boolean state to false (made solely for testing purposes, it works)
    14.             swappedWeapon = false; // set swappedWeapon boolean to false
    15.         }
    Weapon Swap (Secondary to Primary):

    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.Alpha1)) // check if player presses 1
    2.         {
    3.             rotation = player.transform.rotation; // store players rotation before swapping weapon into rotation quaternion, works goooood
    4.  
    5.             if (gunShop.rifleIsPrimary) // check if the rifle is the players primary weapon
    6.             {
    7.                 rifle.SetActive(true);
    8.                 pistol.SetActive(false);
    9.                 revolver.SetActive(false);
    10.                 handcannon.SetActive(false);
    11.                 sMG.SetActive(false);
    12.                 lMG.SetActive(false);
    13.                 burstRifle.SetActive(false);
    14.                 aR.SetActive(false);
    15.                 sUPERTINYPISTOL.SetActive(false);
    16.                 paintBallGun.SetActive(false);
    17.  
    18.                 swappedWeapon = true; // set swappedWeapon boolean to true
    19.             }
    20.             else if (gunShop.sMGIsPrimary) // check if the smg is the players primary weapon
    21.             {
    22.                 rifle.SetActive(false);
    23.                 pistol.SetActive(false);
    24.                 revolver.SetActive(false);
    25.                 handcannon.SetActive(false);
    26.                 sMG.SetActive(true);
    27.                 lMG.SetActive(false);
    28.                 burstRifle.SetActive(false);
    29.                 aR.SetActive(false);
    30.                 sUPERTINYPISTOL.SetActive(false);
    31.                 paintBallGun.SetActive(false);
    32.  
    33.                 swappedWeapon = true; // set swappedWeapon boolean to true
    34.             }
    35.             else if (gunShop.lMGIsPrimary) // check if the lmg is the players primary weapon
    36.             {
    37.                 rifle.SetActive(false);
    38.                 pistol.SetActive(false);
    39.                 revolver.SetActive(false);
    40.                 handcannon.SetActive(false);
    41.                 sMG.SetActive(false);
    42.                 lMG.SetActive(true);
    43.                 burstRifle.SetActive(false);
    44.                 aR.SetActive(false);
    45.                 sUPERTINYPISTOL.SetActive(false);
    46.                 paintBallGun.SetActive(false);
    47.  
    48.                 swappedWeapon = true; // set swappedWeapon boolean to true
    49.             }
    50.             else if (gunShop.burstRifleIsPrimary) // check if the burst rifle is the players primary weapon
    51.             {
    52.                 rifle.SetActive(false);
    53.                 pistol.SetActive(false);
    54.                 revolver.SetActive(false);
    55.                 handcannon.SetActive(false);
    56.                 sMG.SetActive(false);
    57.                 lMG.SetActive(false);
    58.                 burstRifle.SetActive(true);
    59.                 aR.SetActive(false);
    60.                 sUPERTINYPISTOL.SetActive(false);
    61.                 paintBallGun.SetActive(false);
    62.  
    63.                 swappedWeapon = true; // set swappedWeapon boolean to true
    64.             }
    65.             else if (gunShop.aRIsPrimary) // check if the ar is the players primary weapon
    66.             {
    67.                 rifle.SetActive(false);
    68.                 pistol.SetActive(false);
    69.                 revolver.SetActive(false);
    70.                 handcannon.SetActive(false);
    71.                 sMG.SetActive(false);
    72.                 lMG.SetActive(false);
    73.                 burstRifle.SetActive(false);
    74.                 aR.SetActive(true);
    75.                 sUPERTINYPISTOL.SetActive(false);
    76.                 paintBallGun.SetActive(false);
    77.  
    78.                 swappedWeapon = true; // set swappedWeapon boolean to true
    79.             }
    80.             else if (gunShop.pBGunIsPrimary) // check if the paintball gun is the players primary weapon
    81.             {
    82.                 rifle.SetActive(false);
    83.                 pistol.SetActive(false);
    84.                 revolver.SetActive(false);
    85.                 handcannon.SetActive(false);
    86.                 sMG.SetActive(false);
    87.                 lMG.SetActive(false);
    88.                 burstRifle.SetActive(false);
    89.                 aR.SetActive(false);
    90.                 sUPERTINYPISTOL.SetActive(false);
    91.                 paintBallGun.SetActive(true);
    92.  
    93.                 swappedWeapon = true; // set swappedWeapon boolean to true
    94.             }
    95.         }
    Weapon Swap (Primary to Secondary):

    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.Alpha2)) // check if player presses 2
    2.         {
    3.             rotation = player.transform.rotation; // store players rotation before swapping weapon into rotation quaternion, works goooood
    4.  
    5.             if (gunShop.pistolIsSecondary) // check if the pistol is the players secondary weapon
    6.             {
    7.                 rifle.SetActive(false);
    8.                 pistol.SetActive(true);
    9.                 revolver.SetActive(false);
    10.                 handcannon.SetActive(false);
    11.                 sMG.SetActive(false);
    12.                 lMG.SetActive(false);
    13.                 burstRifle.SetActive(false);
    14.                 aR.SetActive(false);
    15.                 sUPERTINYPISTOL.SetActive(false);
    16.                 paintBallGun.SetActive(false);
    17.  
    18.                 swappedWeapon = true; // set swappedWeapon boolean to true
    19.             }
    20.             else if (gunShop.revolverIsSecondary) // check if the revolver is the players secondary weapon
    21.             {
    22.                 rifle.SetActive(false);
    23.                 pistol.SetActive(false);
    24.                 revolver.SetActive(true);
    25.                 handcannon.SetActive(false);
    26.                 sMG.SetActive(false);
    27.                 lMG.SetActive(false);
    28.                 burstRifle.SetActive(false);
    29.                 aR.SetActive(false);
    30.                 sUPERTINYPISTOL.SetActive(false);
    31.                 paintBallGun.SetActive(false);
    32.  
    33.                 swappedWeapon = true; // set swappedWeapon boolean to true
    34.             }
    35.             else if (gunShop.handcannonIsSecondary) // check if the handcannon is the players secondary weapon
    36.             {
    37.                 rifle.SetActive(false);
    38.                 pistol.SetActive(false);
    39.                 revolver.SetActive(false);
    40.                 handcannon.SetActive(true);
    41.                 sMG.SetActive(false);
    42.                 lMG.SetActive(false);
    43.                 burstRifle.SetActive(false);
    44.                 aR.SetActive(false);
    45.                 sUPERTINYPISTOL.SetActive(false);
    46.                 paintBallGun.SetActive(false);
    47.  
    48.                 swappedWeapon = true; // set swappedWeapon boolean to true
    49.             }
    50.             else if (gunShop.sUPERTINYPISTOLIsSecondary) // check if the SUPER TINY PISTOL is the players secondary weapon
    51.             {
    52.                 rifle.SetActive(false);
    53.                 pistol.SetActive(false);
    54.                 revolver.SetActive(false);
    55.                 handcannon.SetActive(false);
    56.                 sMG.SetActive(false);
    57.                 lMG.SetActive(false);
    58.                 burstRifle.SetActive(false);
    59.                 aR.SetActive(false);
    60.                 sUPERTINYPISTOL.SetActive(true);
    61.                 paintBallGun.SetActive(false);
    62.  
    63.                 swappedWeapon = true; // set swappedWeapon boolean to true
    64.             }
    65.         }
    Excuse my messy code, I'm fairly new to coding and this is the best way that I know to doing this. Thank you for the help.

    Edit: I solved it :dddddd
     
    Last edited: Apr 20, 2021
  2. kishorekumarsurya

    kishorekumarsurya

    Joined:
    Aug 10, 2020
    Posts:
    1
    Hi, I have same problem. How to fix this?