S
S
Senture2018-04-15 17:26:08
Unity
Senture, 2018-04-15 17:26:08

How to synchronize GameObject(bullet) in multiplayer on Unity?

Hello, please tell me how to synchronize the bullet with all connected clients? In a multiplayer shooter on Unity (I use UnityNetwork). does anyone have an example?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
GavriKos, 2018-04-15
@Senture

Maybe you will synchronize the leaves on the bushes? To synchronize a bullet, it is enough to send to all clients: departure time, departure coordinates, direction. And then they draw it themselves
. In general, no one usually visualizes bullets with models. Strongly expensive.

G
Griboks, 2018-04-15
@Griboks

Read of. online game tutorial.

S
Senture, 2018-04-16
@Senture

Implemented like this:

spoiler
[SyncVar] Vector3 syncBulletStartPosition;
    [SyncVar] Quaternion syncBulletStartRotation;
    [SyncVar] bool syncTrueFire;

    /// <summary>
    /// Метод для осуществления стрельбы
    /// </summary>
    private void Update()
    {
        if(isLocalPlayer)
        {
            if (Input.GetButton("Fire1"))
            {
                //CmdShoot();

                int weapon = 0;
                // Create the Bullet from the Bullet Prefab
                GameObject bullet = (GameObject)Instantiate(
                    bulletPref[weapon],
                    bulletPivot[weapon].position,
                    bulletPivot[weapon].rotation);


                CmdSendBullet(bulletPivot[weapon].position, bulletPivot[weapon].rotation);
            }
        }
    }

    [Command]
    void CmdSendBullet(Vector3 startPosition, Quaternion startRotation)
    {
        syncBulletStartPosition = startPosition;
        syncBulletStartRotation = startRotation;
        syncTrueFire = true;
    }

    /// <summary>
    /// Вызов всех еобходимых методов для смены и синхронизации оружия
    /// </summary>
    void FixedUpdate()
    {
        SpawnBulletToAllClients();
    }

    void SpawnBulletToAllClients()
    {
        if (!isLocalPlayer)
        {
            if (syncTrueFire == true)
            {
                int weapon = 0;

                GameObject bullet = (GameObject)Instantiate(
                    bulletPref[weapon],
                    bulletPivot[weapon].position,
                    bulletPivot[weapon].rotation);

                CmdSyncTrueFire();
                syncTrueFire = false;
            }
        }
    }

    [Command]
    void CmdSyncTrueFire()
    {
        syncTrueFire = false;
    }

PS Thank you very much to everyone, special thanks to GavriKos

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question