Search Unity

can someone help me? (noob problem)

Discussion in 'Editor & General Support' started by TalesKun, Jan 27, 2020.

  1. TalesKun

    TalesKun

    Joined:
    Jan 27, 2020
    Posts:
    2
    Im new using Unity, i was doing an FPS Game (following some tutorials) but when i made an "weapon switching" this bug happened



    when i use my secondary weapon, i cant move my camera down or up, only left and right

    i can normally shoot, aim, and etc, just cant move my camera down or up

    So, i thought it was an coding issue and i changed the code to the "Brackeys" one, it worked the same and with the same problem, can someone help me?

    the code im using for it follows below

    (my discord: ⎝⧹Tales-Kun ⧸⎠╱.#4679)




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WeaponManager : MonoBehaviour
    6. {
    7.     public int selectedWeapon = 0;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         SelectWeapon();
    12.  
    13.     }
    14.  
    15.  
    16.     // Update is called once per frame
    17.      void Update()
    18.     {
    19.         int previousSelectedWeapon = selectedWeapon;
    20.  
    21.        if (Input.GetAxis("Mouse ScrollWheel") >0f)
    22.         {
    23.             if (selectedWeapon >= transform.childCount - 1)
    24.                 selectedWeapon = 0;
    25.             else
    26.             selectedWeapon++;
    27.         }
    28.         if (Input.GetAxis("Mouse ScrollWheel") < 0f)
    29.         {
    30.             if (selectedWeapon <= transform.childCount - 1)
    31.                 selectedWeapon = transform.childCount -1;
    32.             else
    33.                 selectedWeapon--;
    34.         }
    35.  
    36.  
    37.         if (Input.GetKeyDown(KeyCode.Alpha1))
    38.         {
    39.             selectedWeapon = 0;
    40.         }
    41.         if (Input.GetKeyDown(KeyCode.Alpha2) && transform.childCount >= 2)
    42.         {
    43.             selectedWeapon = 1;
    44.         }
    45.  
    46.         if (previousSelectedWeapon != selectedWeapon)
    47.         {
    48.             SelectWeapon();
    49.         }
    50.     }
    51.  
    52.     void SelectWeapon()
    53.     {
    54.         int i = 0;
    55.         foreach (Transform weapon in transform)
    56.         {
    57.             if (i == selectedWeapon)
    58.                 weapon.gameObject.SetActive(true);
    59.             else
    60.                 weapon.gameObject.SetActive(false);
    61.             i++;
    62.         }
    63.     }
    64.  
    65.    
    66. }
    67.  
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    A possible explanation is that camera movement controls - somehow - are attached to the 1st weapon. So when you disable it, it disables camera control.

    Beyond that it is not quite possible to tell what's wrong without debugging your project.
     
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,157
    If the official script has the same problem then the problem has nothing to do with that script. Like @neginfinity mentioned we would need to see more of the project to be able to determine the problem. Starting with what other scripts are attached to the weapon that is being disabled as well as any child objects as they will be disabled too.
     
    TalesKun likes this.
  4. TalesKun

    TalesKun

    Joined:
    Jan 27, 2020
    Posts:
    2
    (image to help understand)
    Well, my movement script is attached to my "FPS Controller"
    and my weapon manager to the "Weapon Holder", the guns are only childs inside it, my code allows to enable one and disable the another

    but there is literally no diferrence between the weapons, the only thing that changes is the actual gun model, the scripts and everything are literally Ctrl+C and Ctrl+V

    the error isn't on the weapon itself, because if i change the order of them on the scene this bug happens with the another gun (who was normal)

    upload_2020-1-27_23-19-8.png
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    Sure, but something else could be referencing one of those and not the other. If changing the order of the guns has the same bug occur with a different object then something like this is going on.

    Show us the contents of the Inspector for one of your guns.