Search Unity

Third Party Unity3D Photon not spawning player on platform

Discussion in 'Multiplayer' started by chefCoder, Apr 25, 2021.

  1. chefCoder

    chefCoder

    Joined:
    May 24, 2020
    Posts:
    8
    So, recently I've been trying to make a multiplayer game, it has been going pretty smoothly but then came the part of loading in the player to a scene from a lobby menu... It just doesn't work, I've tried quite a few things and they don't seem to work. My issue seems to be that when the player clicks on play and loads into the room they don't spawn on the platform I have made, instead spawning underneath it falling forever... I tried to change region settings but that doesn't seem to work and there isn't much help for my problem. Any help would be greatly appreciated! Attached bellow is the code needed.
    - Chef
    NewtworkManager.cs:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Photon.Pun;
    using Photon.Realtime;

    public class NetworkManager : MonoBehaviourPunCallbacks
    {
    // Start is called before the first frame update
    void Start()
    {
    Connect();
    }

    private void Awake()
    {
    PhotonNetwork.AutomaticallySyncScene = true;
    }

    public void Connect()
    {
    PhotonNetwork.ConnectUsingSettings();
    }

    public void Play()
    {
    PhotonNetwork.JoinRandomRoom();
    }

    public override void OnJoinRandomFailed(short returnCode, string message)
    {
    Debug.Log("Tried to join a room and failed...");
    //if no room
    PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = 6 });
    }

    public override void OnJoinedRoom()
    {
    Debug.Log("Room joined succsess!");
    if (PhotonNetwork.IsMasterClient)
    {
    PhotonNetwork.LoadLevel(1);
    }
    }

    // Update is called once per frame
    void Update()
    {

    }
    }

    PlayerMovement.cs:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Photon.Pun;

    public class PlayerMovement : MonoBehaviourPunCallbacks
    {

    public CharacterController controller;

    public float speed = 8f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    // Update is called once per frame
    void Update()
    {


    isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

    if(isGrounded && velocity.y < 0)
    {
    velocity.y = -2f;
    }

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);

    if(Input.GetButtonDown("Jump") && isGrounded)
    {
    velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    }

    velocity.y += gravity * Time.deltaTime;

    controller.Move(velocity * Time.deltaTime);
    }
    }

    GameManager.cs:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Photon.Pun;

    public class GameManager : MonoBehaviourPunCallbacks
    {

    public GameObject playerPrefab;
    // Start is called before the first frame update
    void Start()
    {
    PhotonNetwork.Instantiate(playerPrefab.name, new Vector3(2, 10, 2), Quaternion.identity);
    }

    // Update is called once per frame
    void Update()
    {

    }
    }
     
    Last edited: Apr 26, 2021
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    When you network instantiate your player have you made sure that its position will be above the ground? It sounds like it's be instantiated below the ground.

    Also please post your code samples in the thread in code tags.
     
    tobiass likes this.
  3. chefCoder

    chefCoder

    Joined:
    May 24, 2020
    Posts:
    8
    Ah, thank you very much! I'll put the code in code tags now as well.