Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

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:
    199
    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.
     
    alecmack_unity likes this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    1,861
    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:
    5
    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.