Search Unity

Strange camera behavior when using Network Lobby (Asset Store)

Discussion in 'Multiplayer' started by soul667, Feb 17, 2017.

  1. soul667

    soul667

    Joined:
    Feb 16, 2017
    Posts:
    5
    I've created a small multiplayer game to get familiar with unity and multiplayer. I've started with the Network Manager component and got it all to work.

    The following screenshot shows how it should look (and it does without the lobby). You can see the ray coming from the camera and hit the groundplane. I use the ScreenPointToRay(Input.mousePosition) method as input. The player always turns in the direction of the mouse cursor.
    multiplayer_correct_ray.png

    If I add the lobby to the menu scene and start the game the following happens (see screen). The ray which should start from the main camera now has it's origin in the player. I only have one camera in this scene and you can see that it's above the player and looks to the ground like in the working example screenshot. This only happens on the host player.
    multiplayer_wrong_ray.png

    The following snippets are from my Player.cs which handles my input for the player. You can see where i draw the debug ray.
    Code (CSharp):
    1.   Camera viewCamera;
    2.  
    3.   // Use this for initialization
    4.   void Start ()
    5.   {
    6.     viewCamera = Camera.main;
    7.   }
    8.  
    9. void Update ()
    10. {
    11. // Movement input
    12. Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    13. Vector3 moveVelocity = moveInput.normalized * moveSpeed;
    14. controller.Move(moveVelocity);
    15.  
    16. // Look input
    17. //viewCamera = GameObject.Find("Main Camera").GetComponent<Camera>(); // tested while debugging
    18. Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
    19. Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
    20.  
    21. float rayDistance;
    22. if (groundPlane.Raycast(ray, out rayDistance))
    23. {
    24.    Rect screenRect = new Rect(0, 0, Screen.width, Screen.height);
    25.    if (screenRect.Contains(Input.mousePosition))
    26.    {
    27.      Vector3 point = ray.GetPoint(rayDistance);
    28.      Camera[] cams = Camera.allCameras;
    29.  
    30.      Debug.DrawLine(ray.origin, point, Color.red);
    31.      controller.LookAt(point);
    32.  
    33.  
    While debugging i found out that the viewCamera.transform has it's position in the player. But obviously the camera is not in the player. The coordinates shown in the inspector while selecting the camera in the hierarchy are correct.
    To be sure I reference the right camera in the code I switched it off with viewCamera.gameObject.setActive(false) and my view was gone. I also can switch between the working and non working version by just disable the LobbyManager object and enable the NetworkManager (and vice versa) in the Hierarchy.

    I have no clue what the LobyManager does to my hosts camera. Can anyone give mit a hint what I'm doing wrong. Just let me know when I described my problem unclear or I should provide more code.

    Thanks for help
    Peter
     
  2. soul667

    soul667

    Joined:
    Feb 16, 2017
    Posts:
    5
    Hi everyone

    I'm still stuck on this. Does really no one has a clue what might be the problem? Or is my question to stupid and I miss somethin obvious? Just give me a hint.

    Thanks
     
  3. soul667

    soul667

    Joined:
    Feb 16, 2017
    Posts:
    5
    I'm a little closer to the problem. I disabled my LateUpdate in the cameraController script. Now I noticed that the camera is somehow moved to the players position. Because my LateUpdate set the position always to the desired coordinates I didn't noticed this effect.
    But I still don't know why this happens and why it happens only on the host.