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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved MiniMap in Multiplayer Game

Discussion in 'Netcode for GameObjects' started by TruckerJoe, Apr 22, 2023.

  1. TruckerJoe

    TruckerJoe

    Joined:
    Feb 25, 2019
    Posts:
    35
    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!
     
  2. CodeNinja-

    CodeNinja-

    Unity Technologies

    Joined:
    Feb 22, 2023
    Posts:
    26
    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
    Code (CSharp):
    1. if(!IsOwner) { return; }
    Note: I'm assuming the mini map camera is in the prefab of the payer.
    Let me know if that worked!
     
  3. TruckerJoe

    TruckerJoe

    Joined:
    Feb 25, 2019
    Posts:
    35
    Hi CodeNinja!

    Thank you for your reply, but I don´t get it work correct.
    Here is my code.
    Code (CSharp):
    1. [SerializeField] private Image miniMapRawImage;
    2.     [SerializeField] private Shader shader;
    3.     [SerializeField] private Camera miniCamera;
    4.     private RenderTexture renderTexture;
    5.  
    6.  
    7.     public override void OnNetworkSpawn()
    8.     {
    9.         if (!IsOwner) return;
    10.  
    11.         renderTexture = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32);
    12.         renderTexture.Create();
    13.  
    14.         renderTexture.Release();
    15.  
    16.         miniCamera.targetTexture = renderTexture;
    17.      
    18.         CreateMaterial(renderTexture);
    19.     }
    20.  
    21.     private void CreateMaterial(RenderTexture renderTexture)
    22.     {
    23.         Material material = new Material(shader);
    24.         material.mainTexture = renderTexture;
    25.  
    26.         miniMapRawImage.material = material;
    27.     }
    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. :-/
     
  4. CodeNinja-

    CodeNinja-

    Unity Technologies

    Joined:
    Feb 22, 2023
    Posts:
    26
    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:

    Code (CSharp):
    1. using Unity.Netcode;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class MiniMap : NetworkBehaviour
    6. {
    7.     [SerializeField] private Shader shader;
    8.     [SerializeField] private Camera miniCamera;
    9.     private RenderTexture renderTexture;
    10.  
    11.  
    12.     public override void OnNetworkSpawn()
    13.     {
    14.         if (!IsOwner) return;
    15.  
    16.         renderTexture = new RenderTexture(256, 256, 16, RenderTextureFormat.ARGB32);
    17.         renderTexture.Create();
    18.  
    19.         renderTexture.Release();
    20.  
    21.         miniCamera.targetTexture = renderTexture;
    22.  
    23.         Material material = new Material(shader);
    24.         material.mainTexture = renderTexture;
    25.  
    26.         GameObject miniMapImageGO = GameObject.FindGameObjectWithTag("MiniMap");
    27.         miniMapImageGO.GetComponent<Image>().material = material;
    28.     }
    29. }
    Would that make it work for you?
     
  5. TruckerJoe

    TruckerJoe

    Joined:
    Feb 25, 2019
    Posts:
    35
    Did you tested it with more then 1 player?
     
  6. CodeNinja-

    CodeNinja-

    Unity Technologies

    Joined:
    Feb 22, 2023
    Posts:
    26
    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?
     
  7. TruckerJoe

    TruckerJoe

    Joined:
    Feb 25, 2019
    Posts:
    35
    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.
     
    CodeNinja- likes this.
  8. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    432
    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