S
S
Senture2018-03-31 14:05:02
Unity
Senture, 2018-03-31 14:05:02

[Unity] Shooting in multiplayer?

Hello!
I have a PlayerShoot script:
This is one of its methods:

[Command]
    void CmdShoot()
    {
        // Create the Bullet from the Bullet Prefab
        GameObject bullet = Instantiate(
            bulletPref,
            bulletSpawn.position,
            bulletSpawn.rotation);

        // Add velocity to the bullet
        bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 700;

        //// Spawn the bullet on the Clients
        NetworkServer.Spawn(bullet);

        // Destroy the bullet after 2 seconds
        Destroy(bullet, 2.0f);
    }

This method is called when clicking on the LMB.
Everything works fine except for one BUT, the spawn of a bullet occurs on the server and is transmitted to the client (as I understand it) and when I connect to the server (remotely in another city), then when shooting from the client, the bullet spawns with a delay after the shot and if I walk (character) then the bullet will spawn outside of the weapon.
5abf6a235c9da043328292.png
this is an example of how to spawn a bullet when walking right to left. I need to do this: the bullet should spawn on the client and be transmitted to the server. I tried to do like this:
void Shoot()
    {
        // Create the Bullet from the Bullet Prefab
        GameObject bullet = Instantiate(
            bulletPref,
            bulletSpawn.position,
            bulletSpawn.rotation);

        // Add velocity to the bullet
        bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 700;

        //// Spawn the bullet on the Clients
        // NetworkServer.Spawn(bullet);
       CmdSpawn(bullet);
        // Destroy the bullet after 2 seconds
        Destroy(bullet, 2.0f);
    }

    [Command]
    void CmdSpawn(GameObject bullet)
    {
        //Spawn the bullet on the Clients
        NetworkServer.Spawn(bullet);
    }

But it throws this error:
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Networking.NetworkServer.GetNetworkIdentity (UnityEngine.GameObject go, UnityEngine.Networking.NetworkIdentity& view) (at C:/buildslave/unity/build/Extensions /Networking/Runtime/NetworkServer.cs:1078)
UnityEngine.Networking.NetworkServer.SpawnObject (UnityEngine.GameObject obj) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1293)
UnityEngine.Networking .NetworkServer.Spawn (UnityEngine.GameObject obj) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1525)
PlayerShoot.CmdSpawn (UnityEngine.GameObject bullet) (at Assets/Scripts/PlayerShoot. cs:156)
PlayerShoot.InvokeCmdCmdSpawn (UnityEngine.Networking.NetworkBehaviour obj, UnityEngine.Networking.NetworkReader reader)
UnityEngine.Networking.NetworkIdentity.HandleCommand (Int32 cmdHash, UnityEngine.Networking.NetworkReader reader) (at C:/buildslave/unity/build/Extensions/Networking /Runtime/NetworkIdentity.cs:618)
UnityEngine.Networking.NetworkServer.OnCommandMessage (UnityEngine.Networking.NetworkMessage netMsg) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1281)
UnityEngine.Networking .NetworkConnection.HandleReader (UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:469)
UnityEngine.Networking.NetworkConnection.HandleBytes (System.Byte[] buffer, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:425)
UnityEngine.Networking.NetworkConnection. TransportReceive (System.Byte[] bytes, Int32 numBytes, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:576)
UnityEngine.Networking.NetworkServer.OnData (UnityEngine.Networking. NetworkConnection conn, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:738)
UnityEngine.Networking.NetworkServer+ServerSimpleWrapper.OnData (UnityEngine.Networking.NetworkConnection conn, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1869)
UnityEngine.Networking. NetworkServerSimple.HandleData (Int32 connectionId, Int32 channelId, Int32 receivedSize, Byte error) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServerSimple.cs:384)
UnityEngine.Networking.NetworkServerSimple.Update() (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServerSimple.cs:247)
UnityEngine.Networking.NetworkServer.InternalUpdate() (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs :691)
UnityEngine.Networking.NetworkServer.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:642)
UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () (at C:/buildslave/unity/ build/Extensions/Networking/Runtime/NetworkIdentity.cs:1091)
How to fix?
PS Thank you so much!!!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2018-03-31
@Senture

Colleague, you are a star. Truth. As a result of the torment, you will come to the idea that the time on the server is the present, and the time on the client is always the past. And it is late by 2 * (ping to the server). Here is a brief description of how Valve implemented it.

H
Herman Coffman, 2019-10-12
@Gera01

There is a unity asset pack with weapons already configured for shooting.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question