Search Unity

How to make object invisible to a specific camera?

Discussion in 'Multiplayer' started by Lekret, Feb 23, 2021.

  1. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    358
    I think it's pretty common multiplayer question. I want players not being able to see their own mesh, but being able to see each other. Is there an easy way to achieve that? I can't use layers since Player prefab is the same for all other players and if they can't see themselves then they automatically won't be able to see each other as well.

    The one possible solution I see is to change camera render distance, but then I need to create another camera to draw my weapon. And what if I want to use different meshes/animations from 1st and 3rd person like it's in most games?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Why would your game client even have the cameras of the other players? On each client there is just the 1 camera, the camera for the player on that client. So you don't even need the mesh renderer for the client's own mesh enabled on any client. No need to mess with cameras. You'd just check for isLocalPlayer, if true you disable mesh renderer for anything on the object you want invisible.

    If you have a 2 camera solution, such as your 1st person and 3rd person cameras, you could just enable the mesh renderer when you switch to 3rd person, and disable when you switch to 1st person. All this is done just on each individual client, not on the server, and not something you propagate over the network.

    I notice a lot of people when they start with networking are confused a bit about how the syncing works. Often people try to think of the game as 1 large shared game, 1 shared state. That is not the case. The server/host and all clients are all completely separate, and you have to think of them that way or it is really easy to get confused as to what is happening and how to solve problems. Nothing gets synced to other clients unless you specifically do so.

    Once you get that understood, solutions to problems like this are easier to get to. You need to do something on one client but not affect the other clients? Easy, just don't sync that thing, basically do nothing and problem solved :) Sharing something you change is actually much more difficult than making a local only change you don't sync.

    Good luck with your game
     
    Last edited: Feb 24, 2021
    Lekret likes this.
  3. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    358
    Thanks, I would spend much more time trying to find workaround and use different crutches. After a couple of attepmts I managed to make everything work as expected, two different players running with their own cameras.
     
    Joe-Censored likes this.