MiniMap in Multiplayer Game

Hi!

I need a MiniMap in my multiplayer game, so I put a camera above my player and look down to him, and set the output of the camera to a texture.
In the HUD I added a rawimage and set the Texture to the texture that the camera is writing.
In Singleplayer, this is working very well, but when I play in multiplayer every player gets the same MiniMap from 1 client.
I think all player using the same texture.
Can anyone tell me how I can fix that, or have an good idea how to make a MiniMap in multiplayer games?

THX!

Hi TruckerJoe,

Indeed, all players seems to be writing in the same texture asset.

You could try to add a NetworkBehavior script to the the mini map camera where you:
- Generate the render texture
- Set that texture as the targetTexture of the mini map camera
- Generate the material with this render texture
- Set it as the material of the mini map image
- And do not forget to check if you are the owner

if(!IsOwner) { return; }

Note: I'm assuming the mini map camera is in the prefab of the payer.
Let me know if that worked!

Hi CodeNinja!

Thank you for your reply, but I don´t get it work correct.
Here is my code.

[SerializeField] private Image miniMapRawImage;
    [SerializeField] private Shader shader;
    [SerializeField] private Camera miniCamera;
    private RenderTexture renderTexture;


    public override void OnNetworkSpawn()
    {
        if (!IsOwner) return;

        renderTexture = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32);
        renderTexture.Create();

        renderTexture.Release();

        miniCamera.targetTexture = renderTexture;

        CreateMaterial(renderTexture);
    }

    private void CreateMaterial(RenderTexture renderTexture)
    {
        Material material = new Material(shader);
        material.mainTexture = renderTexture;

        miniMapRawImage.material = material;
    }

If I play alone, the map is showing but it is flickering while I move my player.
If I play with 2 players, the hole screen gets the minimap image, and the host has a minimap on top displayed and the client not. :-/

You may be missing setting up the material in the UI.
I copy/pasted your code, added a MiniMap tag on the Image where I render the mini map (canvas child) and everything seems working as expected.

Here is my code if you want to compare:

using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;

public class MiniMap : NetworkBehaviour
{
    [SerializeField] private Shader shader;
    [SerializeField] private Camera miniCamera;
    private RenderTexture renderTexture;


    public override void OnNetworkSpawn()
    {
        if (!IsOwner) return;

        renderTexture = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32);
        renderTexture.Create();

        renderTexture.Release();

        miniCamera.targetTexture = renderTexture;

        Material material = new Material(shader);
        material.mainTexture = renderTexture;

        GameObject miniMapImageGO = GameObject.FindGameObjectWithTag("MiniMap");
        miniMapImageGO.GetComponent<Image>().material = material;
    }
}

Would that make it work for you?

Did you tested it with more then 1 player?

Yes I did, I used the external tool ParrelSync to test with two players and each one has it's own map.

Is it not working for you? If not, could you share the project so I can check what's going wrong?

No, it was not working for me, but I made a different way. I use now a fixed texture instead of the camera, and now it is working as expected.
Thank you very much.

1 Like

Only the local player should have the minimap camera enabled. Everybody else should not have the camera at all, and the rendering to the minimap should be disabled.
The same applies to movement, camera controls, etc... All these need to be enabled for the local player in OnNetworkSpawn