John C
John C

Reputation: 517

C#: Unity Photon doesn't add physics to the second connection

I am trying to make a multiplayer air hockey game in Unity, but encountered some problems. When the first user creates the room, the hockey puck is spawned along with the first player's mallet (the object you hit the puck with). When the second player connects to the Photon server, their mallet is spawned.

The issue is when the second player hits the puck with his mallet, the Rigidbody physics don't work on the puck. The puck slides right along the mallet, but doesn't bounce off the mallet.

I am new to Unity, so I don't know what part of the code to post. But this is the code where I spawn the items:

private void Start() {
    Vector2 puckPosition = new Vector2(0,0);
    Vector2 randPos = new Vector2(Random.Range(minX, maxX), Random.Range(minY, maxY));

    if(CreateAndJoinRooms.intHold == 0) { // If the user created the room, the result is 0
       PhotonNetwork.Instantiate(puckPrefab.name, puckPosition, Quaternion.identity);
    }
    
    PhotonNetwork.Instantiate(playerPrefab.name, randPos, Quaternion.identity);
}

Mallet Prefab:

Mallet

Puck Prefab:

Puck

Upvotes: 3

Views: 586

Answers (2)

Paul Skaggs
Paul Skaggs

Reputation: 1

It sounds like the problem is with Photons object ownership. According to the images, the puck prefabs Ownership Transfer = Fixed, which means it will never change. So it's is spawned and controlled by player 1 (as the Master Client), and player 1 is responsible for moving/controlling it.

In this case, Player 1 spawns the puck, player 2 strikes it and affects its Rigidbody2D, but then the next PhotonView Transform & Rigidbody2D updates are received, which set the location & velocity according to player 1s view, ignoring player 2s impact.

A some possible solutions:

  1. Setup a Photon RPC or Photon Event that Player 2 calls to let the Master Client know an impact has happened and to adjust the physics.
  2. Change the OwnerShip Transfer to Take Over, and when a collision is detected have the colliding player take ownership of the puck.

Upvotes: 0

acornTime
acornTime

Reputation: 278

You probably need to add a RigidBodyView component

Upvotes: 1

Related Questions