Spawn bullet from Camera [Netcode]


I have a problem with the host and client cameras, when I want to spawn a bullet from a camera point, the host works fine, the bullet goes in the right direction, but on the client the bullet is shot from the host camera .

I have a script in the camera that disables this if it is not the owner, so each player sees with their own camera, that works fine

Script Shooting

void Update()
    if (!IsOwner) return;
    if (Input.GetMouseButtonDown(0))
private void ShootServerRpc()
    GameObject go = Instantiate(proyectile, spawnProyectile.position, spawnProyectile.rotation);
    go.GetComponent<Rigidbody>().AddForce(spawnProyectile.forward * speedShot); 

In Unity, when using the Photon networking library, the position and orientation of a game object are only updated on the local client if the client is the owner of that game object. This means that if a game object is owned by the host and you try to access its transform from a client, the transform will still reflect the host’s version of the game object, not the client’s version.

To fix this issue, you can make sure that the bullet game object is owned by the client who is shooting it, not the host. This way, when the client accesses the transform of the bullet game object, it will get the transform of the client’s version of the game object, and the bullet will be shot from the client’s camera.

To do this, you can use the PhotonNetwork.Instantiate function to instantiate the bullet game object, and pass in the ownership option OwnershipOption.Takeover as the fourth parameter. This will make the client who calls Instantiate the owner of the newly instantiated game object.

Here is a code reference:
// The position and rotation of the bullet
Vector3 bulletPosition = Camera.main.transform.position;
Quaternion bulletRotation = Camera.main.transform.rotation;

// Instantiate the bullet game object and make the client who calls this function the owner of the bullet
GameObject bullet = PhotonNetwork.Instantiate(“Bullet”, bulletPosition, bulletRotation, 0, null, null, OwnershipOption.Takeover);

// Add force to the bullet in the direction it is facing
bullet.GetComponent().AddForce(bullet.transform.forward * bulletForce);

Thanks for reply

I fixed it by attaching client network transform to bullet prefab