Search Unity

  1. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  2. Unity 2017.1 is now released.
    Dismiss Notice
  3. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  4. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

Unity Multiplayer uNet first person camera issue

Discussion in 'Multiplayer Networking' started by Kobaltic1, Sep 24, 2016.

  1. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    43
    I went through the uNet tut found here: https://unity3d.com/learn/tutorials/topics/multiplayer-networking
    Everything worked great. I am now going back through and making my own game. I used the lobby instead. I am also making a FPS. I am using the standard assets fps controller. I deleted the main camera out of the scene. I added a camera as a child of my player prefab. I add a public Camera to the controller and I dropped in the camera from the prefab. I deleted the code that gets the main camera. I start a server and a client. When I am on the client everything works correctly. When I am on the server, the player moves and shoots but I am looking through the camera of the client and not the server. I also cannot control the camera orbit or movement. I can't figure out why it isn't using the camera on the server player prefab. I have the camera set to untagged. If I disconnect the client, the server prefabs pops to normal and I can control the character correctly. Here is the beginning of the player controller with my changes. The rest of the code is the same as the standard asset.

    Code (csharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityStandardAssets.CrossPlatformInput;
    4. using UnityStandardAssets.Utility;
    5. using Random = UnityEngine.Random;
    6. using UnityEngine.Networking;
    7.  
    8. namespace UnityStandardAssets.Characters.FirstPerson
    9. {
    10.     [RequireComponent(typeof (CharacterController))]
    11.     [RequireComponent(typeof (AudioSource))]
    12.     public class FirstPersonController : NetworkBehaviour
    13.     {
    14.         [SerializeField] private bool m_IsWalking;
    15.         [SerializeField] private float m_WalkSpeed;
    16.         [SerializeField] private float m_RunSpeed;
    17.         [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
    18.         [SerializeField] private float m_JumpSpeed;
    19.         [SerializeField] private float m_StickToGroundForce;
    20.         [SerializeField] private float m_GravityMultiplier;
    21.         [SerializeField] private MouseLook m_MouseLook;
    22.         [SerializeField] private bool m_UseFovKick;
    23.         [SerializeField] private FOVKick m_FovKick = new FOVKick();
    24.         [SerializeField] private bool m_UseHeadBob;
    25.         [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
    26.         [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
    27.         [SerializeField] private float m_StepInterval;
    28.         [SerializeField] private AudioClip[] m_FootstepSounds;    // an array of footstep sounds that will be randomly selected from.
    29.         [SerializeField] private AudioClip m_JumpSound;           // the sound played when character leaves the ground.
    30.         [SerializeField] private AudioClip m_LandSound;           // the sound played when character touches back on ground.
    31.  
    32.         public Camera m_Camera;
    33.         private bool m_Jump;
    34.         private float m_YRotation;
    35.         private Vector2 m_Input;
    36.         private Vector3 m_MoveDir = Vector3.zero;
    37.         private CharacterController m_CharacterController;
    38.         private CollisionFlags m_CollisionFlags;
    39.         private bool m_PreviouslyGrounded;
    40.         private Vector3 m_OriginalCameraPosition;
    41.         private float m_StepCycle;
    42.         private float m_NextStep;
    43.         private bool m_Jumping;
    44.         private AudioSource m_AudioSource;
    45.         public GameObject bulletPrefab;
    46.         public Transform bulletSpawn;
    47.  
    48.  
    49.         // Use this for initialization
    50.         private void Start()
    51.         {
    52.  
    53.             m_CharacterController = GetComponent<CharacterController>();
    54.          
    55.             m_OriginalCameraPosition = m_Camera.transform.localPosition;
    56.             m_FovKick.Setup(m_Camera);
    57.             m_HeadBob.Setup(m_Camera, m_StepInterval);
    58.             m_StepCycle = 0f;
    59.             m_NextStep = m_StepCycle/2f;
    60.             m_Jumping = false;
    61.             m_AudioSource = GetComponent<AudioSource>();
    62.             m_MouseLook.Init(transform , m_Camera.transform);
    63.         }
    64.  
    65.         public override void OnStartLocalPlayer()
    66.         {
    67.             GetComponent<MeshRenderer>().material.color = Color.blue;
    68.         }
    69.         // Update is called once per frame
    70.         private void Update()
    71.         {
    72.             if (!isLocalPlayer)
    73.             {
    74.                 return;
    75.             }
    76.  
    77.  
    78.             if (Input.GetMouseButtonDown(0))
    79.             {
    80.  
    81.                  CmdFire();
    82.             }
    83.  
     
  2. Faltren

    Faltren

    Joined:
    Jan 10, 2017
    Posts:
    17
    I have exactly the same issue, did you find a solution ?
     
  3. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    193
    so is the problem that you have two cameras in the scene and it's rendering the wrong one for the local player?

    if so, in Awake, you could disable the camera. then in OnStartLocalPlayer, turn the camera back on. this function should only be called for the local player, so you'll end up with a situation where the camera for the local player will be enabled, and the remote players' cameras will be disabled.
     
  4. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    43
    I never found a solution. I did not try robochase's solution.
    Yes there is more than one camera. Every player has a camera so in a 16 vs 16 there would be 32 cameras in the scene.
     
  5. Faltren

    Faltren

    Joined:
    Jan 10, 2017
    Posts:
    17
    I tried to disable all cameras an enable it on start and it works. I did this :
    private Camera cam; (Attribute)

    cam = GetComponentInChildren<Camera>(); (in Start())

    if (isLocalPlayer) (in Update())
    {
    if (!cam.enabled)
    cam.enabled = true;
    }
     
  6. Kobaltic1

    Kobaltic1

    Joined:
    Jan 22, 2015
    Posts:
    43
    I ended up doing this in the update. not sure which way is better. This is working and tested with 3 different players.
    Code (csharp):
    1.  
    2. void Update()
    3.     {
    4.         if (isLocalPlayer)
    5.         {
    6.             if (!cam.enabled)
    7.                 cam.enabled = true;
    8.         }
    9.         else
    10.         {
    11.             cam.enabled = false;
    12.         }
    13.     }
    14.