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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Strange behavior of cameras

Discussion in 'Editor & General Support' started by captaincartman91, Jan 22, 2020.

  1. captaincartman91

    captaincartman91

    Joined:
    Jan 22, 2020
    Posts:
    2
    Hello everybody,

    im new to unity and am struggling with a strange behavior of my Cameras.
    Im programming an Online Multiplayer Game with Photon Plugin.

    The strange behavior:
    Player1 logs in -> all good
    Player2 logs in -> Camera of Player1 switches to Player2 character

    Camera of Player2 switches to Player1 character
    Player3 logs in -> Camera of Player 1 and Player2 switches to Player3 character
    Camera of Player3 switches to Player2 character.
    I got a Prefab of the character, which got a camera attached to itself in the inspector.

    And here is my Code for initialization:
    Code (CSharp):
    1. public class NetworkPlayer  : Photon.Pun.MonoBehaviourPun, Photon.Pun.IPunObservable
    2. {
    3.     public Animator anim;
    4.     private Vector3 correctPlayerPos;
    5.     private Quaternion correctPlayerRot;
    6.     public GameObject myCam;
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         if(photonView.IsMine){
    11.             Debug.Log("PhotonView.IsMine == true");
    12.             //Kamera und Steuerung aktivieren
    13.             myCam = GameObject.Find("Camera");
    14.             myCam.SetActive(true);
    15.             GetComponent<PlayerMovement>().enabled = true;
    16.             Debug.Log("Steuerung und Cam aktiviert...");
    17.         }
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if(!photonView.IsMine){
    24.             transform.position = Vector3.Lerp(transform.position, this.correctPlayerPos, Time.deltaTime*5);
    25.             transform.rotation = Quaternion.Lerp(transform.rotation, this.correctPlayerRot, Time.deltaTime * 5);
    26.         }
    27.     }
    28.  
    29.     //Exchange Position data
    30.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
    31.         if(stream.IsWriting){
    32.             //Send data to others
    33.             stream.SendNext(transform.position);
    34.             stream.SendNext(transform.rotation);
    35.             stream.SendNext(anim.GetBool("Run"));
    36.         } else {
    37.             //Receive data from others
    38.             this.correctPlayerPos = (Vector3) stream.ReceiveNext();
    39.             this.correctPlayerRot = (Quaternion) stream.ReceiveNext();
    40.             anim.SetBool("Run", (bool) stream.ReceiveNext());
    41.         }
    42.     }
    43. }
    I also tried to attach the camera via the inspector instead of searching for it, like in the code sample above.
    Hope anyone can help me with this :(

    Thank you for your time!



     
  2. captaincartman91

    captaincartman91

    Joined:
    Jan 22, 2020
    Posts:
    2
    Solution: I forgot to disable the camera in the prefab!