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

Can see gun through walls?

Discussion in 'Scripting' started by AtomicCabbage33, Jan 25, 2016.

  1. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    I can see the others player's gun through walls even though I have 2 cameras set up. The gun does not clip you can see the gun through the entire map. Any ideas? The main cameras culling mask is everything except the gun layer and the second camera ( the gun camera) is setup like so:
     

    Attached Files:

  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    You're rendering every gun in the scene last (Depth 1), which means they will be drawn on top of everything else. Each player's main camera needs to render their own gun last to prevent it from going through the environment when standing near an obstacle, but the other players' guns need to be rendered in order by depth, which means they should be rendered with the rest of the scene.

    One way you could do this is have more than one renderer on each player GameObject, one on the Gun layer and one on the Default (or whatever) layer. Then just use scripting to turn off the one that shouldn't be seen depending on whether that character belongs to the player.
     
  3. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    Okay that makes sense, guess I'll be making a script then cheers
     
  4. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    I added a section of code to my 'Player' script which should switched the gun's layer over to the default one if they are not a local player, thus stopping the glitch. However, it is not working. The debug log prints out the 'I'm working' log but I can still see the other players guns through walls?
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class Player : NetworkBehaviour {
    6.  
    7.     [SyncVar]
    8.     private bool _isDead = false;
    9.     public bool isDead
    10.     {
    11.         get { return _isDead; }
    12.         protected set { _isDead = value; }
    13.     }
    14.  
    15.     [SerializeField]
    16.     private int maxHealth = 100;
    17.  
    18.     [SyncVar]
    19.     public int currentHealth;
    20.  
    21.     public GUIStyle deathGUI;
    22.  
    23.     [SerializeField]
    24.     private Behaviour[] disableOnDeath;
    25.     private bool[] wasEnabled;
    26.  
    27.     public GameObject gun;
    28.  
    29.     public void Start()
    30.     {
    31.         if (!isLocalPlayer)
    32.         {
    33.             gameObject.layer = LayerMask.NameToLayer ("Default");
    34.             Debug.Log("I'm working :D");
    35.         }
    36.     }
    37.  
    38.     public void Setup ()
    39.     {
    40.         wasEnabled = new bool[disableOnDeath.Length];
    41.         for (int i = 0; i < wasEnabled.Length; i++)
    42.         {
    43.             wasEnabled[i] = disableOnDeath[i].enabled;
    44.         }
    45.  
    46.         SetDefaults();
    47.     }
    48.  
    49.     //void Update ()
    50.     //{
    51.     //    if (!isLocalPlayer)
    52.     //        return;
    53.  
    54.     //    if (Input.GetKeyDown(KeyCode.K))
    55.     //    {
    56.     //        RpcTakeDamage(99999);
    57.     //    }
    58.     //}
    59.  
    60.     [ClientRpc]
    61.     public void RpcTakeDamage (int _amount)
    62.     {
    63.         if (isDead)
    64.             return;
    65.  
    66.         currentHealth -= _amount;
    67.  
    68.         Debug.Log(transform.name + " now has " + currentHealth + " health.");
    69.  
    70.         if (currentHealth <= 0)
    71.         {
    72.             Die();
    73.         }
    74.     }
    75.  
    76.     private void Die()
    77.     {
    78.         isDead = true;
    79.  
    80.         for (int i = 0; i < disableOnDeath.Length; i++)
    81.         {
    82.             disableOnDeath[i].enabled = false;
    83.         }
    84.  
    85.         Collider _col = GetComponent<Collider>();
    86.         if (_col != null)
    87.             _col.enabled = false;
    88.  
    89.         Debug.Log(transform.name + " is DEAD!");
    90.  
    91.         StartCoroutine(Respawn());
    92.     }
    93.  
    94.     private IEnumerator Respawn ()
    95.     {
    96.         yield return new WaitForSeconds(GameManager.instance.matchSettings.respawnTime);
    97.  
    98.         SetDefaults();
    99.         Transform _spawnPoint = NetworkManager.singleton.GetStartPosition();
    100.         transform.position = _spawnPoint.position;
    101.         transform.rotation = _spawnPoint.rotation;
    102.  
    103.         Debug.Log(transform.name + " respawned.");
    104.     }
    105.  
    106.     public void SetDefaults ()
    107.     {
    108.         isDead = false;
    109.  
    110.         currentHealth = maxHealth;
    111.  
    112.         for (int i = 0; i < disableOnDeath.Length; i++)
    113.         {
    114.             disableOnDeath[i].enabled = wasEnabled[i];
    115.         }
    116.  
    117.         Collider _col = GetComponent<Collider>();
    118.         if (_col != null)
    119.             _col.enabled = true;
    120.     }
    121.  
    122.     private void OnGUI()
    123.     {
    124.         if (isDead == true)
    125.         {
    126.             GUI.Box(new Rect(200, 100, Screen.width / 1.2f , Screen.height), transform.name + " has died" , deathGUI);
    127.         }
    128.     }
    129.  
    130. }
    131.  
     
  5. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    What exactly is it doing? Can you see in the inspector that the intended objects are correctly switching to the default layer?

    Did you remember to set the culling mask in your depth only camera to include the local player layer and exclude the 'Default' layer?
     
  6. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    You wouldn't want to set the depth only camera's culling mask to include the local player as the local player's image would be duplicated seeing as the main camera deals with that. And no, the layers aren't switching in the inspector.
     
  7. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    I might not be on the same page as you, but I'm pretty sure the depth only camera should be rendering only the local player's gun/hands, and the main camera should ignore them.

    Main cam: Non-local players' models are drawn with the environment

    Depth only cam: Local player's gun/hands drawn on top of the scene

    My recommendation is still to have a character object contain components for rendering both the things that should be seen by local and the things that shouldn't be seen by local, and selectively enable/disable the renderers instead of switching layers. Local stuff is always on the local layer, other stuff is always on another layer.
     
  8. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    I tried your suggesting about drawing the local player on the depth only cam, it only made the issue worse. T could see the entire player through the scene and their player model appeared to be following my camera.
     
  9. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    An attempt at clarification:

    The depth only cam should only ever render the gun models that should be seen from the first person perspective. Never the full character! That's why I said "gun/hands" earlier. And you need to disable the renderers for those first-person models that exist on non local players, otherwise you will be able to see them as you described through everything.
     
  10. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    I made an example project for you, since I'm having trouble articulating.

    DL Link

    Notice how in Start() the appropriate renderers are disabled based on isLocal. You can try changing isLocal to a different character (make sure only one character is set to local at a time, obviously) to see that it always works.
     
  11. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    Link didnt work man
     
  12. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Sorry, I think I gave you a temp link. Use this one instead.
     
  13. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    What link haha
     
  14. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Click on "this one" in my above post.