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

PUN frustration, can't player is not instantiating across network

Discussion in 'Scripting' started by Lethn, May 15, 2019.

  1. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I'm following a PUN tutorial and it's been frustrating me quite a bit, I'm going through videos by InfoGamer and what he says makes sense but for the life of me I can't get PhotonNetwork.Instantiate to work despite me copying what he's done almost exactly. He uses void Start () to instantiate the player when you have joined a room but when I click play nothing happens.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class QuickStartRoomController : MonoBehaviourPunCallbacks
    7.  
    8. {
    9.     [SerializeField]
    10.     private int multiplayerSceneIndex;
    11.  
    12.     public override void OnEnable()
    13.  
    14.     {
    15.         PhotonNetwork.AddCallbackTarget(this);
    16.     }
    17.  
    18.     public override void OnDisable()
    19.  
    20.     {
    21.         PhotonNetwork.RemoveCallbackTarget(this);
    22.     }
    23.  
    24.     public override void OnJoinedRoom()
    25.  
    26.     {
    27.         StartGame();
    28.     }
    29.  
    30.     public void StartGame()
    31.  
    32.     {
    33.         if (PhotonNetwork.IsMasterClient)
    34.  
    35.         {
    36.             PhotonNetwork.LoadLevel(multiplayerSceneIndex);
    37.         }
    38.     }
    39.  
    40. }
    41.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using Photon.Realtime;
    6. using UnityEngine.UI;
    7.  
    8. public class QuickStartLobbyController : MonoBehaviourPunCallbacks
    9.  
    10. {
    11.     [SerializeField]
    12.     private GameObject quickStartButton;
    13.     [SerializeField]
    14.     private GameObject quickCancelButton;
    15.     [SerializeField]
    16.     private int RoomSize;
    17.  
    18.     private void Start()
    19.  
    20.     {
    21.         PhotonNetwork.ConnectUsingSettings();
    22.     }
    23.  
    24.     public override void OnConnectedToMaster()
    25.  
    26.     {
    27.         PhotonNetwork.AutomaticallySyncScene = true;
    28.         quickStartButton.SetActive(true);
    29.     }
    30.  
    31.     public void QuickStart()
    32.  
    33.     {
    34.         quickStartButton.SetActive(false);
    35.         quickCancelButton.SetActive(true);
    36.         PhotonNetwork.JoinRandomRoom();
    37.     }
    38.  
    39.     public override void OnJoinRandomFailed(short returnCode, string message)
    40.  
    41.     {
    42.         CreateRoom();
    43.     }
    44.  
    45.     public void QuickCancel ()
    46.  
    47.     {
    48.         quickCancelButton.SetActive(false);
    49.         quickStartButton.SetActive(true);
    50.         PhotonNetwork.LeaveRoom();
    51.     }
    52.  
    53.     public void CreateRoom()
    54.  
    55.     {
    56.         int randomRoomNumber = Random.Range(0, 10000);
    57.         RoomOptions roomOps = new RoomOptions() { IsVisible = true, IsOpen = true, MaxPlayers = (byte)RoomSize };
    58.         PhotonNetwork.CreateRoom("Room" + randomRoomNumber, roomOps);
    59.     }
    60.  
    61. }
    62.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5.  
    6. public class GameSetup : MonoBehaviour
    7.  
    8. {
    9.  
    10.     public static GameSetup GS;
    11.     public Transform[] spawnPoints;
    12.  
    13.     private void OnEnable()
    14.  
    15.     {
    16.         if (GameSetup.GS == null)
    17.  
    18.         {
    19.             GameSetup.GS = this;
    20.             Debug.Log("Game Setup Empty Ready");
    21.         }
    22.     }
    23.  
    24. }
    25.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Photon.Pun;
    5. using System.IO;
    6. using Photon.Realtime;
    7. using UnityEngine.UI;
    8.  
    9. public class PhotonPlayer : MonoBehaviour
    10.  
    11. {
    12.     public GameObject localPlayer;
    13.  
    14.     void Start()
    15.  
    16.     {
    17.         CreatePlayer();
    18.     }
    19.  
    20.     private void CreatePlayer()
    21.  
    22.     {
    23.         int spawnPicker = Random.Range(0, GameSetup.GS.spawnPoints.Length);
    24.         localPlayer = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "FPSControllerEmptyPrefab"), GameSetup.GS.spawnPoints[spawnPicker].position, GameSetup.GS.spawnPoints[spawnPicker].rotation, 0);
    25.     }
    26.  
    27.  
    28. }
    29.  
    Just to make sure I wasn't being dumb I made sure because the tutorial uses paths to define where the prefabs are I deleted any other resource folders I could find thinking that was causing the problem and so on but still the players don't instantiate across the network.

    I'm not doing anything complicated right now, just connecting to a random room and having a player spawn in. I have posted in other places like the PUN support forums but honestly they seem rather dead people just don't seem to use them very much.

     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I've just realised I am actually a retard and I completely missed out a step involving attaching the PhotonPlayer script to the GameSetup object so that everything spawns automatically and now it's working. Interestingly though he missed out this step on the player avatar video he did so I wonder if this was a correction but I'll double check all that and see.
     
    SparrowGS likes this.
  3. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    My only problem now if anyone is curious is that I've come onto the same problem again of my two players for some reason copying each other. This shouldn't be the case with the code that I have now that I think about it more the code should all be correct and I'm wondering if the standalone client is reading my input no matter what even if I've clicked onto the editor.

    Has anyone else run into this as an issue? It's another thing that's made testing annoying and I want to make sure my networking is right before I move on.
     
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Did some testing and I found out it was in fact the network movement that was the issue and the players weren't moving separate from each other looks like I need to get researching on that.
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    What do you mean copying each other?, like if one player moves to the left all the players moves to the left?
     
  6. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Yes, despite the code InfoGamer has given my input goes across all player instances.
     
  7. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I can't believe this actually worked, I hate it when I make a really length post and research all the fixes myself. So it turns out that for whatever reason at least in the latest version of PUN 2 PhotonView.IsMine does not work in the update function and I have no idea why. If somebody could explain it to me that would be great, but what I did was I found this answer which seemed incredibly unlikely but it worked for me and I simply wrapped all of my functions in separate IsMine if statements and now it seems to all be working flawlessly with no movement at all from the other player when there shouldn't be.

    Why does nobody make any mention of this in the tutorials or the documentation? I'm actually really infuriated lol.

    https://answers.unity.com/questions/592181/photon-view-problem.html

    For newbies as well and anybody else getting annoyed with PUN, here's the code I ended up with try this and see if it fixes your problems too.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Experimental.Input;
    6. using Photon.Pun;
    7.  
    8. public class FPSController : MonoBehaviourPun
    9.  
    10. {
    11.     private PhotonView PV;
    12.  
    13.     private InputControls inputControls;
    14.  
    15.     private CharacterController cc;
    16.  
    17.     private Vector2 moveAxis;
    18.  
    19.     [SerializeField] private float walkSpeed;
    20.     [SerializeField] private float strafingSpeed;
    21.  
    22.     [SerializeField] private float horizontalSensitivity;
    23.     [SerializeField] private float verticalSensitivity;
    24.  
    25.     [SerializeField] private Transform cameraTransform;
    26.     [SerializeField] private float minCamAngle;
    27.     [SerializeField] private float maxCamAngle;
    28.  
    29.     private float cameraAngle = 0;
    30.  
    31.  
    32.     [SerializeField]private float isJumping;
    33.     [SerializeField]
    34.     private float jumpVelocity;
    35.     private Vector3 jumpVector;
    36.     [SerializeField]
    37.     private float gravityMultiplier;
    38.  
    39.     private bool isCrouching;
    40.     private bool isCrouchBlocked;
    41.  
    42.     private void Awake()
    43.  
    44.     {
    45.         inputControls = new InputControls();
    46.     }
    47.  
    48.     private void OnEnable()
    49.  
    50.     {
    51.         inputControls.FPS.Move.performed += MovementInput;
    52.         inputControls.FPS.Move.Enable();
    53.         inputControls.FPS.MouseX.performed += RotationAxisY;
    54.         inputControls.FPS.MouseX.Enable();
    55.         inputControls.FPS.MouseY.performed += cameraRotationAxisX;
    56.         inputControls.FPS.MouseY.Enable();
    57.         inputControls.FPS.Jump.performed += JumpInput;
    58.         inputControls.FPS.Jump.Enable();
    59.         inputControls.FPS.Crouch.performed += CrouchInput;
    60.         inputControls.FPS.Crouch.Enable();
    61.     }
    62.  
    63.     private void OnDisable()
    64.  
    65.     {
    66.         inputControls.FPS.Move.performed -= MovementInput;
    67.         inputControls.FPS.Move.Disable();
    68.         inputControls.FPS.MouseX.performed -= RotationAxisY;
    69.         inputControls.FPS.MouseX.Disable();
    70.         inputControls.FPS.MouseY.performed -= cameraRotationAxisX;
    71.         inputControls.FPS.MouseY.Disable();
    72.         inputControls.FPS.Jump.performed -= JumpInput;
    73.         inputControls.FPS.Jump.Disable();
    74.         inputControls.FPS.Crouch.performed -= CrouchInput;
    75.         inputControls.FPS.Crouch.Disable();
    76.     }
    77.  
    78.     private void Start()
    79.  
    80.     {
    81.         cc = GetComponent<CharacterController>();
    82.         PV = GetComponent<PhotonView>();
    83.     }
    84.  
    85.     void Update()
    86.  
    87.     {
    88.             PlayerMovement();
    89.  
    90.             if (isCrouching)
    91.  
    92.             {
    93.                 CheckCrouchBlocked();
    94.             }
    95.  
    96.     }
    97.  
    98.     void PlayerMovement()
    99.  
    100.     {
    101.         if (PV.IsMine)
    102.  
    103.         {
    104.  
    105.             Vector3 movement = Vector3.zero;
    106.             movement += transform.forward * moveAxis.y * walkSpeed * Time.deltaTime;
    107.             movement += transform.right * moveAxis.x * strafingSpeed * Time.deltaTime;
    108.  
    109.             movement += Physics.gravity * gravityMultiplier;
    110.  
    111.             if (cc.isGrounded)
    112.  
    113.             {
    114.                 if (isJumping > 0)
    115.  
    116.                 {
    117.                     jumpVector.y = jumpVelocity;
    118.                     isJumping = 0;
    119.                 }
    120.             }
    121.  
    122.             else
    123.  
    124.             {
    125.                 jumpVector += Physics.gravity * gravityMultiplier * Time.deltaTime;
    126.             }
    127.  
    128.             movement += jumpVector;
    129.             movement *= Time.deltaTime;
    130.  
    131.             cc.Move(movement);
    132.  
    133.         }
    134.     }
    135.  
    136.     private void CrouchInput(InputAction.CallbackContext context)
    137.  
    138.     {
    139.         if (PV.IsMine)
    140.  
    141.         {
    142.             if (isCrouchBlocked)
    143.                 return;
    144.             isCrouching = !isCrouching;
    145.  
    146.             if (isCrouching)
    147.  
    148.             {
    149.                 cc.height = 1.54f;
    150.                 cc.center = new Vector3(0, -0.37f, 0);
    151.                 cameraTransform.localPosition = new Vector3(0, -0.08f, 0);
    152.             }
    153.  
    154.             else
    155.  
    156.             {
    157.                 cc.height = 2;
    158.                 cc.center = new Vector3(0, 0, 0);
    159.                 cameraTransform.localPosition = new Vector3(0, 1.27f, 0);
    160.             }
    161.         }
    162.  
    163.     }
    164.  
    165.     private void CheckCrouchBlocked()
    166.  
    167.     {
    168.         if (PV.IsMine)
    169.  
    170.         {
    171.             RaycastHit hit;
    172.  
    173.             if (Physics.Raycast(cameraTransform.position, Vector3.up, out hit, 1f))
    174.  
    175.             {
    176.                 isCrouchBlocked = true;
    177.             }
    178.  
    179.             else
    180.  
    181.             {
    182.                 isCrouchBlocked = false;
    183.             }
    184.  
    185.             Debug.DrawRay(cameraTransform.position, Vector3.up * 1.1f, Color.red);
    186.         }
    187.  
    188.     }
    189.  
    190.     private void JumpInput (InputAction.CallbackContext context)
    191.  
    192.     {
    193.         if (cc.isGrounded)
    194.             isJumping = context.ReadValue<float>();
    195.     }
    196.  
    197.     private void RotationAxisY (InputAction.CallbackContext context)
    198.  
    199.     {
    200.         float yRotate = context.ReadValue<float>();
    201.         transform.Rotate(new Vector3(0, yRotate, 0) * horizontalSensitivity * Time.deltaTime);
    202.     }
    203.  
    204.     private void cameraRotationAxisX (InputAction.CallbackContext context)
    205.  
    206.     {
    207.         cameraAngle += context.ReadValue<float>() * verticalSensitivity * Time.deltaTime;
    208.         cameraAngle = Mathf.Clamp(cameraAngle, minCamAngle, maxCamAngle);
    209.  
    210.         cameraTransform.localEulerAngles = new Vector3(-cameraAngle, 0, 0);
    211.     }
    212.  
    213.     private void MovementInput(InputAction.CallbackContext context)
    214.  
    215.     {
    216.         moveAxis = context.ReadValue<Vector2>();
    217.     }
    218. }
    219.  
    Edit: NOPE! Still unresolved! Going to have to do more testing and see if any other solutions can be found :( That's really annoying unless I'm doing something weird.
     
    Last edited: May 16, 2019
  8. ksc_3899

    ksc_3899

    Joined:
    Jan 1, 2019
    Posts:
    30
    This worked. Just make sure you have added Photon Transform View component to the player and added it to the assigned components of Photon View.