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

Question Different Cameras for Host and Client(s)

Discussion in 'Netcode for GameObjects' started by alecmack_unity, Dec 2, 2022.

  1. alecmack_unity

    alecmack_unity

    Joined:
    Nov 7, 2022
    Posts:
    1
    I have recently started a project in which I do not want separate gameobjects for players, I would like separate cameras looking at different parts of the scene. More specifically, I want the host to be looking at an object already in the scene and any connected clients to be looking at a separate object in the scene. Any suggestions?
     
    bobdabuilder2 likes this.
  2. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    Simple: make the cameras networkobjects, and disable/enable the cameras on each client/host depending which one you want them to look through. You can do this for example in a NetworkBehaviour script on the camera objects with its OnNetworkSpawn method, see the docs.
     
    Hara1 and alecmack_unity like this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,533
    I would use Cinemachine. At a minimum, you'd need a "look at target" virtual camera and simply, through RPC messages, tell clients (this includes the host) which object they should use as the camera's "look at target".

    You need to find a way to specify those target objects though and how to do that depends entirely what type of game you are making, how many objects, whether they get spawned dynamically and so on.
     
    alecmack_unity likes this.
  4. bbangerter

    bbangerter

    Joined:
    Sep 17, 2014
    Posts:
    7
    Just to add to the discussion, I would probably not make your cameras network objects at all. Let each client control their own camera facing, position, etc. Depending on your exact needs this could be controlled through RPC calls to the clients as CodeSmile suggested, or if the individual clients can zoom, rotate, move their own cameras through user input then even the RPC calls probably are not needed. Just hook up each clients camera(s) to the local clients user input.