2D shooter mouse position problem

Hi, im trying to make a 2D multiplayer shooter game. Everything seems to work properly so far except the shooting script.

When a player shoots, my script instantiates a bullet prefab at the players position and then a bullet script adds force to this bullet to mouse position.

It works fine for a single player but when i try with 2 players(1 host and 1 client) client shoots to the mouse position of the host. It works fine for the host but i couldnt make clients to shoot to their own mouse positions.

This is the script attached to the player prefab:

void Update ()
	{
		if(!isLocalPlayer){
			return;
		}else{
			if (Input.GetButton("Fire1") && Time.time > nextLM && Time.time > globalCooldown){
				nextLM = Time.time + cdLM;
				Cmd_LeftMouse();
			}
		}
	}

    [Command]
	private void Cmd_LeftMouse(){
		GameObject shot = (GameObject)Instantiate(shotLM, attackLM.position, attackLM.rotation);
		DestroyObject(shot,1);
		NetworkServer.Spawn(shot);
	}

and this is the script attached to the “shot” prefab:

void Awake(){
	 playershot();
	}
void playerShot(){
		shotPos = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
		GetComponent<Rigidbody2D>().AddForce(shotPos.normalized * shootSpeed,ForceMode2D.Force);
	}

the Cmd_LeftMouse() gets called on the host. The host creates the shot. The host tells the clients to create the shot. So now we have a shot instance on the host and a shot instance on the client. So what positions do they take on? They’ll take the position dictated by the server. In other words, the server controls the positions for the host’s and the client’s “shot” game object (assuming you have “Network Transform” component on the shot game object).

Since you want the shot created by the client to follow the client’s mouse position, you’ll need to have the client constantly tell the host where the client’s mouse position is, and then the host needs to set the force on the shot. Having the client tell the server its mouse position (via another Cmd function, such as “Cmd_UpdatePosition(float x, float y)” should be done frequently, perhaps even every frame. This will use up some bandwidth if you don’t optimize it. This is the easiest way.

The commonly used way is you can have the client’s shot’s position be updated locally by the client who shot it (that way there’s no lag between where the mouse is and when the shot moves toward the mouse), then have the client frequently update the server on the shot’s new position, and then have the server frequently update all other clients about the shot’s position. Unless you do some extrapolation, the shot’s position on the server and the shot’s position on the other clients will be lagging behind. Advantage is that the controlling player doesn’t see any delay. Disadvantages that there is lag between the shot on your screen, and the shot on the server/other client’s screen, it’s can be a bit harder to implement, it’s more prone to hacking (client can just “make up” where the shot is, and server has to accept it).

Hope this helps.