0

I am making 2D Top-down shooter in network. I spawn a gun on Players hand position. My problem is, when Client spawn his gun, it has some kind of delay. When client is moving, the gun is not following him well, just appearing by him within 1/4 Sec. It just "jumps" to player. The problem is only on the client-side, the host is fine.

Gun is in spawnable components, it has Network Identity and authority, its also has Network Transform. Player has same components.

Here is my Code:

public class Player : NetworkBehaviour 
{

    [SerializeField] float speed = 10f;
    [HideInInspector] public GameObject playerGun;
    public GameObject gunPrefab;

    void Update() 
    {
            Movement();
            if (Input.GetKeyDown(KeyCode.I))
            {
                CmdGetGun();
            }
    }

    private void Movement()
    {
        Vector3 position = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized * Time.deltaTime * speed;
        transform.position += position;
        MouseMovement();
    }

    private void MouseMovement()
    {
        Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        mousePosition.Normalize();
        float rotation_z = Mathf.Atan2(mousePosition.y, mousePosition.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rotation_z);
    }

    [Command]
    public void CmdGetGun()
    {
        Debug.Log("SPAWNING A GUN");
        GameObject playerGun = (GameObject)Instantiate(gunPrefab, GetComponentInChildren<HandHolder>().transform.position, GetComponentInChildren<HandHolder>().transform.rotation, GetComponentInChildren<HandHolder>().transform);
        NetworkServer.Spawn(playerGun);
        RpcSetGunOnClients(playerGun);
    }

    [ClientRpc]
    private void RpcSetGunOnClients(GameObject playerGun)
    {
        this.playerGun = playerGun;
    }

Here are Player and Gun components :

Player prefab

Gun Prefab

Im trying to find the solution for days.

I tried even without using NetworkTransform on a gun, but then i had a problem with new players joining server. They couldnt see updated position of already spawned guns.

I hope somebody can help me.

derHugo
  • 83,094
  • 9
  • 75
  • 115
Kiket95
  • 73
  • 10
  • How exactly do you test the connection? Local on the same machine in two instances or on actually two different PC's/devices connected via network? Is it possible that it is simply the network delay * 2 (first from client to host than back to the client)? The host ha no delay since his methods are called immediately. – derHugo Oct 26 '18 at 03:55
  • It is localhost on one PC. – Kiket95 Oct 26 '18 at 10:34
  • Same issue when I test it on localhost one PC and in 2 different PCs via network. Just checked it. – Kiket95 Oct 26 '18 at 11:02
  • The easiest thing to do would be to save on network traffic and make the gun a child object of the player. Assuming that works for your game. If not I have another idea. – Tyler C Oct 26 '18 at 19:16
  • @Kiket95 wasn't what Tyler C said just what I already recommended to you in the `Solution 2` of [my last answer](https://stackoverflow.com/a/52886207/7111561)? – derHugo Oct 27 '18 at 05:54
  • If you look close on your `NetworkBehaviour` component in the Inspector you can see the value `Network Send interval = 0.1` which means: Send data to server in intervals of `0.1 seconds`, server send data to clients in intervals of `0.1 seconds` => `delay = 0.2 seconds` which is more or less your `1/4 second`. You can make it higher (see [sendInterval in NetworkSettings](https://docs.unity3d.com/ScriptReference/Networking.NetworkSettingsAttribute.html)) but be aware that this stresses your networking bandwidth and should be avoided – derHugo Oct 27 '18 at 06:01

0 Answers0