Reputation: 3
I'm making a multiplayer game, and so far I made a player that can be moved, a platform and the framework for multiplayer connection. I've made sure to test the host and client joining together as I built on the project, and once I was done giving each player a camera that follows them and can be rotated, all of a sudden the client doesn't join the game anymore. Clicking the host button puts you in the game and works as intended, but joining afterwards as a client simply doesn't spawn you on the platform. I also don't get any errors. Here are the scripts that might have caused problems: First:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
using Unity.Netcode.Transports.UTP;
[System.Serializable]
public struct PlayerNetworkState : INetworkSerializable
{
public Vector3 Position;
public Vector3 Rotation;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref Position);
serializer.SerializeValue(ref Rotation);
}
}
public class PlayerNetwork : NetworkBehaviour
{
[SerializeField] private bool usingServerAuth;
private NetworkVariable<PlayerNetworkState> playerState;
private Rigidbody playerRb;
private void Awake()
{
playerRb = GetComponent<Rigidbody>();
var permission = usingServerAuth ? NetworkVariableWritePermission.Server : NetworkVariableWritePermission.Owner;
playerState = new NetworkVariable<PlayerNetworkState>(new PlayerNetworkState {Position = Vector3.zero, Rotation = Vector3.zero}, NetworkVariableReadPermission.Everyone, permission);
}
public override void OnNetworkSpawn()
{
if (!IsOwner) {
Destroy(transform.GetComponent<Player>());
Destroy(transform.GetComponent<CamController>());
}
}
private void Update()
{
if (IsOwner) TransmitState();
else ConsumeState();
}
#region Transmit State
private void TransmitState()
{
var state = new PlayerNetworkState
{
Position = playerRb.position,
Rotation = transform.rotation.eulerAngles
};
if (IsServer || !usingServerAuth)
{
playerState.Value = state;
}
else
{
TransmitStateServerRpc(state);
}
}
[ServerRpc(RequireOwnership = false)]
private void TransmitStateServerRpc(PlayerNetworkState state)
{
playerState.Value = state;
}
#endregion
#region Consume State
private void ConsumeState()
{
transform.position = playerState.Value.Position;
transform.rotation = Quaternion.Euler(playerState.Value.Rotation);
}
#endregion
}
Second:
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
public class Player : NetworkBehaviour
{
private Rigidbody playerRb;
public GameObject camHolderPrfb;
public GameObject camHolder;
public float speed;
void Start()
{
camHolder = Instantiate(camHolderPrfb, transform.position, Quaternion.identity);
playerRb = GetComponent<Rigidbody>();
}
void Update()
{
//Makes sure player can only control its character
if (!IsOwner) {return;}
//Calculates player movement
float forwInput = Input.GetAxis("Vertical");
float horzInput = Input.GetAxis("Horizontal");
Vector3 direction = camHolder.transform.forward * forwInput + camHolder.transform.right * horzInput;
//Applies movement to player
if (direction.magnitude > 1) {direction.Normalize();}
playerRb.AddForce(direction * speed);
}
}
Third:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Unity.Netcode;
public class CamController : NetworkBehaviour
{
public float rotationSpeed;
public float lerpSpeed;
public GameObject camHolder;
private Camera playerCamComp;
private GameObject playerCam;
public Player PlayerScr;
void Start()
{
camHolder = PlayerScr.camHolder;
playerCamComp = camHolder.GetComponentInChildren<Camera>();
playerCam = playerCamComp.gameObject;
}
void FixedUpdate()
{
//Camera is rotatable with left and right keys
float camInput = Input.GetAxis("MoveCam");
camHolder.transform.Rotate(Vector3.up, camInput * Time.deltaTime * rotationSpeed);
//Camera moves and looks at the player
camHolder.transform.position = Vector3.Lerp(camHolder.transform.position, transform.position, lerpSpeed * Time.deltaTime);
playerCam.transform.LookAt(this.transform);
}
}
All scripts are assigned to the player. There's also a script that spawns the start host, server and client button and deactivates the main camera, so the player camera can take its place, when one of the buttons is clicked. Let me know if you might need that. There's absolutely no errors that can lead me in the right direction, and I'm not even sure where to add logs for debugging.
Upvotes: 0
Views: 22