How to add Bots

i have a multiplayer game and its working just fine using Unet.
now i wanted when someone create a room, he can control the bots amount in a room,
i have tried to add a test Prefap to network manager spawnble prefap stuff and instantiate it but it didn’t sync across players in game.

what i have in my mind is :
1_the bots take an slot in the room size.
2_the bots navmesh agent must run on host device only, then sync the transform.

well is it possible?

i have tried to at least instantiate it, but it didn’t work at all, it only spawn on host not on client
did any one have this problem before?

You need to add a method who create the bots and spawn them, I don’t know how you do it exactly in Unet

The method needs to run when the hosts start hosting ,

I guess that the bots can’t leave the game if so you can add the the number of bots to the correct number of players connected

finally i got it!!!

to make it working you need to make sure you have Unet Game

1_make an new GameObject and add your stuff on it then add NetworkIdentity on it. DO NOT ENABEL ANY THING ON IT!!!
[194479-image-2022-03-26-172918.png*|194479]
2_Add you GameObject To you NetworkManager Registered Spawnable Prefabs
[194480-2.png*|194480]
we have The AI Prefab Ready Now To Spawn it

"Btw i did it in my own way so you can do it in your way to "

Now to spawn it i made a separate script witch sits on player

using UnityEngine.Networking;
using UnityEngine;

public class BotHandling : NetworkBehaviour //sits on player object
{
    [SyncVar]
    public bool ThisIsTheHostPlayer = false;

    public GameObject Ai;

    void Start()
    {
        if (isLocalPlayer && isServer)
        {
            ThisIsTheHostPlayer = true;
            SpawnBots();
        }
        else
        {
            this.enabled = false;
        }
    }

    void SpawnBots()
    {
        int BotsNumber = 4;
        for(int i = 0;i<BotsNumber;i++)
        {
            Transform point = NetworkManager.singleton.GetStartPosition();
            GameObject bot = Instantiate
            (
                Ai,
                point.position,
                point.rotation
            );

            //i am gona explain this wait for it
            bot.GetComponent<AI_State>().isOnHost = true;

            NetworkServer.Spawn(bot);
        }
    }
}

Witch spawns the Bots

Now they are synced to all players

But they must move Right?

so add this to the AI

using UnityEngine;

public class AI_State : MonoBehaviour
{
    public bool isOnHost;
}

this is important for other scrips

Now we need the AI to move using NavMeshAgent

add NavMeshAgent Component to the Bot and add this script

using UnityEngine.Networking;
using UnityEngine;

public class AI_Setup : NetworkBehaviour
{
    [SerializeField]private Behaviour[] ComponentsToDisable;

    void Start ()
    {
        if(GetComponent<AI_State>().isOnHost)
        {
            EnableState(true);
            GetComponent<AI_Player>().PlayerSetup();
        }
        else
        {
            EnableState(false);
        }
    }

    void EnableState (bool ii)
    {
        for(int i = 0; i < ComponentsToDisable.Length; i++)
		{
			ComponentsToDisable*.enabled = ii;*
  •  }*
    

}
}
this script disable components on client side but enable it on host side
i wanted my bots to work like this
now the components that need to be disabled is
1_NavMeshAgent
2_any script that moves the bot
Lastly add NetworkTransform To Sync Position and stuff
But i still have a problem when you close the room and re create a new room the bots don’t move so its another problem to solve pls if someone have a solution to this please help
anyway thanks for all :slight_smile:
*
*