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.
  2. Dismiss Notice

Question Cinemachine camera in Multiplayer Photon not working

Discussion in 'Cinemachine' started by Mat_Cyber, Jul 31, 2021.

  1. Mat_Cyber

    Mat_Cyber

    Joined:
    Apr 14, 2020
    Posts:
    8
    Hi,

    I am fairly new to the whole multiplayer game mechanics, but I made a simple car controller and added cinemachine camera as a child to the car and then made it into a prefab, and I instantiate the car prefab in the game.....but every time I do that, the camera shifts to the other car.....

    I've added all the important stuff like photon view and transform. I've also changed the script to check 'if.mine',

    but still the camera shifts to the other second player while I control the first....

    Can someone please help me to find a solution......
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Can you show the hierarchy of your prefab?
     
  3. Mat_Cyber

    Mat_Cyber

    Joined:
    Apr 14, 2020
    Posts:
    8
    Hey,

    Here's my hierarchy for the car.....

    upload_2021-8-6_23-46-55.png

    I've placed the camera along with the car prefab and the cinemachine as a child of the main camera of the car.....
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    The problem is in your structure.

    You should have only one Camera, with a CinemachineBrain. This camera should be in the scene, outside of the prefabs. It will be controlled at all times by the current active virtual camera.

    In each prefab, put a Virtual Camera. When it is instanced, the main camera will see it. If the vcam is fixed in relation to the sports car, then it's ok to have it as a child. However, if it's an independent object that tracks the car procedurally with CM's Body and Aim algorithms, it's best if it's a sibling of the sports car rather than a child.
     
  5. Mat_Cyber

    Mat_Cyber

    Joined:
    Apr 14, 2020
    Posts:
    8
    Hey,

    So this might be a very basic question, but does making the camera a child similar to it being the sibling like you mentioned above

    Because I kept the camera in the scene and only kept the vcam as a child to the car prefab. But when I hit play the camera again switches to the other player......

    upload_2021-8-18_10-17-1.png

    This is now my new hierarchy ☝
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    If the vcam has procedural tracking enabled (you can show me the inspector) then ideally it should be a sibling of the car rather than a child, but it shouldn't really matter.

    Can I also see your scene hierarchy?
     
  7. Mat_Cyber

    Mat_Cyber

    Joined:
    Apr 14, 2020
    Posts:
    8
    Oh...

    Well this is the inspector of the vcam,

    upload_2021-8-29_20-1-2.png

    And this is my scene hierarchy,

    upload_2021-8-29_20-2-22.png
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    That all looks correct. When you spawn a car, what I would expect is that the main camera will begin tracking that car, because of the vcam. When you spawn a second car, then the camera will begin tracking that new car, because of the new vcam spawned with the same priority. What is the behaviour you are looking for?
     
  9. Mat_Cyber

    Mat_Cyber

    Joined:
    Apr 14, 2020
    Posts:
    8
    Well I'm playing this in two windows where I spawn one and to the same room I spawn the second player. But the moment I spawn the second player the camera switches to the second player while I control the first player in the first window. At the same time when I switch to the second window the camera is pointing towards the first player while I control the second.

    What I want is the camera in the first window to point towards the first car (the one which I am controlling) and similarly for the second car in the second window

    Can it be because of my code, I am kind of a beginner and don't know a lot about multiplayer system.



    This is the tutorial that I followed....
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    In that case you should not include a vcam in the player prefab. Instead, put the vcam in the scene, and have some custom code that sets the vcam's target to the player you are controlling when it is spawned.
     
  11. Mat_Cyber

    Mat_Cyber

    Joined:
    Apr 14, 2020
    Posts:
    8
    Can you maybe suggest some reference or tutorial where I can check that...
     
  12. unity_zP4G898ky1jT3A

    unity_zP4G898ky1jT3A

    Joined:
    Mar 11, 2023
    Posts:
    25
    When you do this, there is no problem if there is only one camera, but if there is more than one CM camera, the problem still persists.
    I tried both methods you mentioned but the problem persists.
    Even if I leave the camera outside the prefab the problem persists. Do you have any suggestions for a multiplayer system using multiple CM cameras?
    1.PNG 2.PNG 3.PNG 4.PNG 5.PNG

    I have no problem with a single CM camera. The problem occurs when I use more than one CM camera !
     
    Last edited: Oct 17, 2023
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233