Input problems in multiplayer

I’m trying to implement this tutorial Learn on a simple 2D shooter I threw togheter. However, in the game the player aims the gun with the mouse and it works in single player, but on the server it doesn’t register. Thus on the client I can point and shoot. But on the server it only shows as aiming and shooting in one direction. The “movement” code works on the server though.

Just to clarify, on the local screen the own player can aim toward the mouse and fire in that direction. On the other client though it looks like the player only moves around and aim and fire in one direction. This is why I believe it to be a server issue.

I’ll post, what I believe to be, some relevant code that I use.

This code is used for the aiming

// Update is called once per frame
    void Update () {

        //Add a check for isLocalPlayer in the Update function, so that only the local player processes input.
        if (!isLocalPlayer)
        {
            return;
        }

        //Make object aim toward where the mouse pointer is.
        var pos = Camera.main.WorldToScreenPoint(transform.position);
        var dir = Input.mousePosition - pos;
        var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

        //Fire weapon
        if (Input.GetMouseButtonDown(0))
        {
            CmdFireWeapon();
        }
    }


// This [Command] code is called on the Client …
    // … but it is run on the Server!
    [Command]
    void CmdFireWeapon()
    {
        //scr_PistolBullet newBullet = Instantiate(Bullet, FirePosition.position, FirePosition.rotation) as scr_PistolBullet;

        //Network Code
        // Create the Bullet from the Bullet Prefab
       
        var bullet = (GameObject)Instantiate(
            BulletPrefab,
            FirePosition.position,
            FirePosition.rotation);
       
        bullet.GetComponent<Transform>().transform.Translate(Vector2.right * 10.0f * Time.deltaTime);
       
        NetworkServer.Spawn(bullet);
    }

Does anyone know what I do wrong?

Can you show us the code that syncronizes the rotation to the other clients and the server?

I don’t have such code. From the tutorial I thought that running (!isLocalPlayer) would be enough to interpret between client and server. I’m going back to the tutorial to see if I have missed anything.

Or would you perhaps know if there is a simple solution to this problem? The rotation code is at the top under the update() function.

Uhm, you need some code to sync the rotation? It doesn’t automagically do that

I’ve managed to fix the rotation now. Not entirely sure what I did, but one thing was in the player object I changed the Transform sync mode in the network transform component from syncing transform to syncing rigidbody 2D.

However it gave me a new challenge. On the client, the sprite of other players doesn’t follow the player object. That is, I can tell from shooting that an invisible object is moving. But the sprite stays in place. This happens only on the client though. On the host screen it works as intended.

Note, this problem didn’t exist when i was using the mode sync transform.

Any ideas to what is goin wrong?

Hmm, I just noticed that I put the Network Send Rate in the wrong direction. For some reason I thought less is better. It is not. :stuck_out_tongue: