MirrorNetworking / Mirror

#1 Open Source Unity Networking Library
https://mirror-networking.com
MIT License
5.21k stars 769 forks source link

When I join on a separate client, both games switch to that screen and then it breaks. Also the movement script doesn't work. #3192

Closed AHumanIBelieve closed 2 years ago

AHumanIBelieve commented 2 years ago

I am trying to create a battle royale game in Unity, and I have implemented Mirror(or I tried). I have done everything necessary(I think) - Network Identities, network transforms, making all scripts networkbehaviour and transports and managers. Yet this happens:

broken game-2022-07-20_07 32 20 The spaces after entering names was me attempting to move, even though the movement script is definitely attached. The way movement works is that once the name is entered, the name screen is deactivated and movement is enabled. The movement script is under an if(isLocalPlayer) check.

I am using this asset as my player, but I have made it a networkbehaviour script.

Thank you in advance, AHumanIBelieve (Unity 2021.3.2f1, Linux Mint 19.3 & Mirror 66.0.9)

DavidMann10k commented 2 years ago

Does the player prefab have a camera on it? I think you're instantiating a second camera with the player prefab, and the host now having two main cameras, is using it to render.

AHumanIBelieve commented 2 years ago

First of all, thank you!

Secondly, the player does have two cameras - one to see and one for a minimap. So if the player has a camera, the entire thing breaks? How do I fix this so that each player has their own camera but it doesn't break? Thank you!

Player Prefab: image

If you want more information, I'll be happy to provide it.

Thank you!

DavidMann10k commented 2 years ago

The problem is that if you add a second camera to the scene it assumes you want to render from the newly added one. To clarify, you don't need a camera for each player; you need only one for the locally controlled player.

I've seen this solved in two ways. You could change the camera so that is only is active if(isLocalPlayer). Or you can remove the camera from the prefab, create a new camera in the scene, and make it follow the locally controlled player.

I think you might be better helped on Discord.

AHumanIBelieve commented 2 years ago

Thank you! I'll be sure to try it out.

AHumanIBelieve commented 2 years ago

For those finding this thread, you cannot have a main camera on the player prefab. Instead, you should place a camera in the scene. Write a script to auto-attach it to the player - something like this(change variables depending on your player):

using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using Mirror;

public class AttachCamera : NetworkBehaviour
{
    public GameObject Camera;

    void Awake()
    {
        Camera = GameObject.FindWithTag("MainCamera");
        Camera.transform.SetParent(this.transform);
        Debug.Log("camera attached");
        Camera.transform.localPosition = new Vector3(0, 0, 0.6f);
        Debug.Log("camera pos set");
    }
}

And attach it to the player. Make sure the camera is tagged as MainCamera.

Good luck.

JesusLuvsYooh commented 2 years ago

You can have a camera inside player prefab, its not recommended, but if for some reason you do.. Its as simple as disable it by default in prefab, and then local player enables it. Done :D

public GameObject cameraObj;

public override void OnStartLocalPlayer()
{
  cameraObj.SetActive(true);
}
AHumanIBelieve commented 2 years ago

In fact, just follow this.

I sometimes hate my life so much.