Search Unity

multiplayercamera is broken

Discussion in 'Multiplayer' started by harpingseal, Oct 19, 2021.

  1. harpingseal

    harpingseal

    Joined:
    Sep 3, 2020
    Posts:
    57
    i use mirror to create a multiplayer game.So Once i press host+client and connect a client to it , The first client will be locked to a view,Input not responding ans such,Is there any fix?The script is basic camera rotation with movement and islocal player check.Every time a new client join,The client that connect before will lose control.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5.  
    6. public class CameraLook : NetworkBehaviour
    7. {
    8.     private Vector3 rotation;
    9.     public float lookSpeed;
    10.     public GameObject GameObjectTransform;
    11.     public GameObject Camera;
    12.  
    13.     public float lookXLimit = 60.0f;
    14.     public float ZoomAmount = 0;
    15.     public float MaxToClamp = 100;
    16.     public float ROTSpeed = 10;
    17.     public Transform camTransform;
    18.     public ParticleSystem particle;
    19.     public ParticleSystem Shock;
    20.     private ParticleSystem pscom;
    21.     private ParticleSystem Shockcom;
    22.     public GameObject spaceship;
    23.     private FOUDemo fou;
    24.     void Start()
    25.     {
    26.         fou = spaceship.GetComponent<FOUDemo>();
    27.         pscom = particle.GetComponent<ParticleSystem>();
    28.         Shockcom = Shock.GetComponent<ParticleSystem>();
    29.  
    30.     }
    31.     void Update()
    32.     {
    33.         if (isLocalPlayer == false) return;
    34.  
    35.         if (Input.GetMouseButton(1))
    36.         {
    37.             rotation.y += Input.GetAxis("Mouse X") * lookSpeed;
    38.             rotation.x += -Input.GetAxis("Mouse Y") * lookSpeed;
    39.             rotation.x = Mathf.Clamp(rotation.x, -lookXLimit, lookXLimit);
    40.             GameObjectTransform.transform.localRotation = Quaternion.Euler(rotation.x, rotation.y, 0);
    41.         }
    42.         ZoomAmount += Input.GetAxis("Mouse ScrollWheel");
    43.         ZoomAmount = Mathf.Clamp(ZoomAmount, -MaxToClamp, MaxToClamp);
    44.         var translate = Mathf.Min(Mathf.Abs(Input.GetAxis("Mouse ScrollWheel")), MaxToClamp - Mathf.Abs(ZoomAmount));
    45.         Camera.transform.Translate(0, 0, translate * ROTSpeed * Mathf.Sign(Input.GetAxis("Mouse ScrollWheel")));
    46.         if (Input.GetKey("space"))
    47.         {
    48.  
    49.             var emission = pscom.emission;
    50.             emission.enabled = true;
    51.             fou.velocity = 5000;
    52.         }
    53.         if (Input.GetKeyUp("space"))
    54.         {
    55.             var emission = pscom.emission;
    56.             emission.enabled = false;
    57.             var emission2 = Shockcom.emission;
    58.             emission2.enabled = true;
    59.             Shockcom.Stop();
    60.             Shockcom.Play();
    61.  
    62.             fou.velocity = 500;
    63.  
    64.         }
    65.         if (Input.GetKey("w"))
    66.         {
    67.  
    68.             fou.thrust = fou.thrust + 1;
    69.  
    70.         }
    71.         if (Input.GetKey("s"))
    72.         {
    73.  
    74.             fou.thrust = fou.thrust - 1;
    75.  
    76.         }
    77.  
    78.     }
    79. }
     
    Last edited: Oct 19, 2021
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Line 11 implies you are setting the reference to the Camera object in the inspector, and the Camera object is part of this prefab. This will result in multiple active cameras in the scene. You either need to disable all these extra cameras, or the better solution is to not include the camera as part of a networked prefab at all. Instead have the local player prefab find the scene camera and reposition it when it gets set up on its client, and of course don't do that when on other clients.

    If the above is a wrong guess, please explain how your camera(s) are set up.