Bullets Not Firing Correctly Over Network

I've been banging my head against a problem for many hours now. In my project, I have two players over the network that create a Plane in a script and raycast to it to determine where the mouse was clicked. Whenever a player clicks, a bullet is created via RPC:

[code2=csharp]void Start () {

m_ClickPlane = new Plane(-Vector3.forward, Vector3.zero);


}[/code2]

[code2=csharp]void Update() {

if(Input.GetMouseButton(0) && canShoot_ == true && photonView.isMine)
{

photonView.RPC("shootBullet", PhotonTargets.All, bulletName);
}

}[/code2]

In the shootBullet function, I determine where the bullet should spawn and fire based on the information received from the ray:

[code2=csharp][RPC]
void shootBullet(string bulletName){

ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float dist;

if (m_ClickPlane.Raycast(ray, out dist))

{
Vector3 dir = (ray.GetPoint(dist) - this.transform.position).normalized;

GameObject bullet = Instantiate (prefab_, this.transform.position + dir, Quaternion.identity) as GameObject;

BulletComponent bul = bullet.GetComponent<BulletComponent>();

bul.setBulletForce(new Vector2(dirToShoot.x, dirToShoot.y) * bulletSpeed_);
bul.fireBullet();

}
}[/code2]

On the screen of the player that fired the bullet, the bullets are spawning and moving correctly. However, for the other players, the bullet spawn location does not change (so they all spawn from the same spot). For example:

Player 1 shoots. Player 1 sees their bullet firing correctly. Player 2 does not. (See attachments)

Is there something I'm forgetting to do for this? Any help would be greatly appreciated.

Comments

  • Can't read the code now but from the images, I wonder:
    Do you send where you shoot in which direction? Or does each client check where the mouse is and fires at it?
  • Both. The clients each check where the mouse is relative to the player character. Then when the clients click, the bullet is created and given a force; the bullet has a script that keeps track of its own position via OnPhotonSerializeView.
  • You should not synchronize bullets with OnPhotonSerializeView.
    You can have hundreds of bullets and all simply move in a straight line. It's not worth sending this info.
    Do an RPC and send where you shot in which direction and which type of bullet. Maybe add when it happened. Then any client can calculate where the bullet is. Don't instantiate, sync and destroy bullets.