Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Weapon still clips even with 2 camera setup?

Discussion in 'Scripting' started by AtomicCabbage33, Feb 14, 2016.

  1. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    In my game I have 2 cameras, one is the main camera which draws everything except the gun and a scond camera which is depth only and draws the gun and nothing else. When I change the gun's layer to 'weapon' it works and the gun no longer clips so I know that my cameras are set up correctly, however my game is multiplayer so with that current setup you would be able to see other players gun through the scene. To solve this issue I have added some code to my 'player shoot' script which changes the weapons layer to the 'weapon' layer if we are a local player, thus in theory preventing clipping. However, the guns layer is changing from the default layer to the 'weapon' layer when the game starts, as it should, but for some strange reason the main camera is still drawing the gun and the second camera isn't?? Any ideas :/
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Are you sure you changed the layer of the gameobject that has the renderer on it?
     
  3. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    What renderer are you referring to?
     
  4. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    The renderer for the local player's weapon.
     
  5. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    The local player's weapon is comprised of multiple objects, each with their own renderer and so I attached the weapon to a prefab and am changing the layer of the prefab. I realised that my script only changes the layer of the first object, and not the children which have the renderers attached. How can I adapt my script so it changes the layers of the children as well? This is an extract from my code which deals with the cameras:

    Code (CSharp):
    1. [SerializeField]
    2.     private GameObject weaponGFX;
    3.     [SerializeField]
    4.     private string weaponLayerName = "Weapon";
    5.  
    6.  
    7.     void Start ()
    8.     {
    9.         if (cam == null)
    10.         {
    11.             Debug.LogError("PlayerShoot: No camera referenced!");
    12.             this.enabled = false;
    13.         }
    14.  
    15.         weaponGFX.layer = LayerMask.NameToLayer (weaponLayerName);
    16.     }
     
    Last edited: Feb 16, 2016
  6. joewode

    joewode

    Joined:
    Dec 3, 2014
    Posts:
    35
    You could tag each part of the weapon and use the tag to generate a list. Then you could use a for loop to switch the layer of each object in the list:
    Code (CSharp):
    1.     [SerializeField]
    2.         private GameObject[] weaponGFX;
    3.         [SerializeField]
    4.         private string weaponLayerName = "Weapon";
    5.  
    6.  
    7.         void Start ()
    8.         {
    9.             weaponGFX = GameObject.FindGameObjectsWithTag ("WhateverTag");
    10.  
    11.             if (cam == null)
    12.             {
    13.                 Debug.LogError("PlayerShoot: No camera referenced!");
    14.                 this.enabled = false;
    15.             }
    16.  
    17.             for (int i = 0; i < weaponGFX.Length; i++) {
    18.                  weaponGFX[i].layer = LayerMask.NameToLayer (weaponLayerName);
    19.             }
    20.         }
    Hope that helps
     
    Last edited: Feb 16, 2016
  7. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Code (csharp):
    1.  
    2. public static void SetLayerRecursively( Transform transform, int layer )
    3. {
    4.      transform.gameObject.layer = layer;
    5.      for( int i = 0; i < transform.childCount; ++i )
    6.      {
    7.          SetLayerRecursively( transform.GetChild(i), layer );
    8.      }
    9. }
    10.  
     
  8. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    Cheers Antony Blackett and joewode, my gun no longer clips but the other players can see my gun through the scene which I thought would be solved by switching the layers. :/
     
  9. joewode

    joewode

    Joined:
    Dec 3, 2014
    Posts:
    35
    Last edited: Feb 16, 2016
  10. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    on the camera that renders the guns over everything, set its far clip plane to something like 1 meter, enough to see your gun but not enough to see anyone else's... It's a bit of a hack, but might be good enough.
     
  11. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    I have added some code so if you are not the local player, it assigns the gun the 'dontdraw
    But I want to be able to see the other players gun, just not through walls.
     
  12. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
  13. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    I found the script which switches the guns material over if they are not a local player and tried to incorporate it into my game, however all it does is quite literally switch the guns material over, which doesn't solve the issue as you can still see their gun through the scene? hmmm : /