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

Why Do I spawn as the other player? And how do I fix it?

Discussion in 'Scripting' started by Real_BTN4, Oct 22, 2020.

  1. Real_BTN4

    Real_BTN4

    Joined:
    Oct 16, 2020
    Posts:
    23
    So I have a few scripts for Photon. They work but for some reason, I control player 2 on my screen, and on my brother's screen he controls player 2. How do I fix this? Here are the scripts.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Photon.Realtime;
    6.  
    7. public class PhotonManager : MonoBehaviourPunCallbacks
    8. {
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         PhotonNetwork.ConnectUsingSettings();
    13.     }
    14.  
    15.  
    16.    
    17.     public override void OnConnectedToMaster()
    18.     {
    19.         PhotonNetwork.JoinLobby();
    20.     }
    21.  
    22.     public override void OnJoinedLobby()
    23.     {
    24.         PhotonNetwork.JoinOrCreateRoom("Room",new RoomOptions {MaxPlayers=4},TypedLobby.Default);
    25.  
    26.     }
    27.  
    28.  
    29.     public override void OnJoinedRoom()
    30.     {
    31.        
    32.         PhotonNetwork.Instantiate("Player", new Vector2(Random.Range(-8f,11f), transform.position.y), Quaternion.identity);
    33.     }
    34. }


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public float speed;
    8.  
    9.     private float x;
    10.     private float y;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         x = Input.GetAxis("Horizontal");
    21.         y = Input.GetAxisRaw("Vertical");
    22.         transform.position += (Vector3)new Vector2(x * speed * Time.deltaTime, 0);
    23.         transform.position += (Vector3)new Vector2(0, y * speed * Time.deltaTime);
    24.        
    25.     }
    26. }
    27.  


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class PlayerNetworking : MonoBehaviour
    7. {
    8.     public MonoBehaviour[] scriptsToIgnore;
    9.  
    10.     private PhotonView photonView;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         photonView = GetComponent<PhotonView>();
    15.        if (!photonView.IsMine)
    16.        {
    17.            foreach (var script in scriptsToIgnore)
    18.            {
    19.                script.enabled = false;
    20.            }
    21.        }
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.        
    28.     }
    29. }
    30.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    In PlayerMovement.cs you are taking input and moving the player, without checking if this is your client's player. I'm assuming this script is attached to all players, and not just your own player.
     
  3. Real_BTN4

    Real_BTN4

    Joined:
    Oct 16, 2020
    Posts:
    23
    It is attached to all players.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Well you don't want to all clients to control all players, which appears to be how it is written. You want the clients to just control their own player.
     
  5. Real_BTN4

    Real_BTN4

    Joined:
    Oct 16, 2020
    Posts:
    23
    Ok. How do I do that? I'm a little new but I want to do this.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Sorry, I'm not that familiar with Pun, so I don't know what the right way to check this is. (In Unet or Mirror you would check for isLocalPlayer, there is probably something similar for Pun)

    But in your third script PlayerNetworking, it looks like it is set up to disable any scripts which you add to the scriptsToIgnore array. The solution might be as simple as adding PlayerMovement to that array in the inspector if it is not already there. If it is already there, you should verify that the PlayerMovement component is actually disabled on all Player objects where which the client does not own. (if that is the case, then the problem is probably unrelated to anything I've suggested)