Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Photon Synching issues

Discussion in 'Multiplayer' started by tidalgaming, Mar 3, 2021.

  1. tidalgaming

    tidalgaming

    Joined:
    Feb 27, 2021
    Posts:
    1
    Heya,

    Been trying to get this to work for two days now. It's my first multiplayer game so it's a bit of a learning curve.
    Currently I have it setup so that players can create a room and join. Players can move and their cameras are all correct. The issue i'm having is with the balls. The game is essentially a dodgeball game but the balls are not synching up at all. The balls can be picked up and then thrown which will cause a knockback on hit; however when a player picks up a ball, for the other players the ball does not move, once thrown the ball goes off in a direction for one player and a different direction for the others. I've used photon view and photon rigidbody on the balls to try to track their movement and update the server but it seems to be getting it completely wrong. I've also tried transform view and I am now initializing the 4 balls at the start of the game.

    Please help! :(

    Code (CSharp):
    1. From my game object:
    2.         balls = GameObject.FindGameObjectsWithTag("pickupable");
    3.         Debug.Log(balls.Length);
    4.         if(ballSpawnPoints == null && balls.Length == 0){
    5.             ballSpawnPoints = GameObject.FindGameObjectsWithTag("BallRespawn");
    6.             for(int i = 0; i < ballSpawnPoints.Length; i++){
    7.                 PhotonNetwork.Instantiate(ballPrefab.name, ballSpawnPoints[i].transform.position, Quaternion.identity, 0);
    8.             }
    9.         }
    10.  
    11. From my player controller:
    12. void Update()
    13.     {
    14.        
    15.         if(photonView.isMine){
    16.             Movement();
    17.             look();
    18.             if (Input.GetMouseButtonDown(0)){
    19.                 photonView.RPC("Cast", PhotonTargets.All);
    20.             }
    21.             if(pickUp.heldObj != null){
    22.                 pickUp.heldObj.GetComponent<Rigidbody>().velocity = Vector3.zero;
    23.                 pickUp.heldObj.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
    24.                 Debug.Log(pickUp.heldObj.GetComponent<Rigidbody>().velocity);
    25.                 photonView.RPC("moveObj", PhotonTargets.All);
    26.             }
    27.         }
    28.  
    29.      
    30.     }
    31.  
    32.     [PunRPC]
    33.     void Cast(){
    34.         Debug.Log("Casting");
    35.         pickUp.Cast();
    36.     }
    37.    
    38.     [PunRPC]
    39.     void moveObj(){
    40.         Debug.Log("moving");
    41.         pickUp.MoveObject();
    42.     }
    43.  
    44. From my pickup script:
    45.  
    46.     public void Cast(){
    47.         if(heldObj == null){
    48.             RaycastHit hit;
    49.             if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, pickUpRange) && hit.transform.tag=="pickupable"){
    50.                 heldObj = hit.transform.gameObject;
    51.                 PickupObject(hit.transform.gameObject);
    52.                
    53.             }
    54.         }
    55.         else{
    56.             DropObject();
    57.         }
    58.     }
    59.  
    60.     public void MoveObject(){
    61.         if(Vector3.Distance(heldObj.transform.position, holdParent.position) > 0.1f){
    62.             Vector3 moveDirection = (holdParent.transform.position - heldObj.transform.position);
    63.             heldObj.GetComponent<Rigidbody>().AddForce(moveDirection *moveForce);
    64.         }
    65.     }
    66.  
    67.     private void PickupObject(GameObject pickObj){
    68.         if(pickObj.GetComponent<SphereCollider>()){
    69.             pickObj.GetComponent<SphereCollider>().enabled = false;
    70.         }
    71.         if(pickObj.GetComponent<Rigidbody>()){
    72.             Rigidbody objRig = pickObj.GetComponent<Rigidbody>();
    73.             objRig.useGravity = false;
    74.             objRig.drag = 10;
    75.             objRig.transform.parent = holdParent;
    76.             pickObj.transform.position = holdParent.transform.position;
    77.             objRig.velocity = Vector3.zero;
    78.             objRig.angularVelocity = Vector3.zero;
    79.             heldObj = pickObj;
    80.  
    81.         }
    82.     }
    83.  
    84.     private void DropObject(){
    85.         if(heldObj.GetComponent<SphereCollider>()){
    86.             heldObj.GetComponent<SphereCollider>().enabled = true;
    87.         }
    88.         Rigidbody heldRig = heldObj.GetComponent<Rigidbody>();
    89.         heldRig.useGravity = true;
    90.         heldRig.drag = 0;
    91.         heldObj.transform.parent = null;
    92.         heldObj = null;
    93.        
    94.         heldRig.AddForce(transform.forward * ThrowForce);
    95.     }
    96. }
    Thanks !
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,062
    Dodgeball is a very physical and fast game. This is not easy to do as online multiplayer game and PUN is admittedly not providing tools to simplify the task.
    Keep in mind that there is a delay between sending a message and receiving it. From the perspective of the Physics engine, this is a lot of time and there is plenty of room for the clients to disagree about millimeters or more of positioning. This again, is enough for the physics to violently resolve overlaps...

    You might have to make the objects kinematic. Not driven by the physics engine. At least on the machines that don't control the balls! Otherwise the engine will counter the incoming networked positions with catastrophic results.

    I can't read / debug the code at the moment. Do as you'd usually do with debugging: Start with the simplest piece of code and when that works, add the other pieces of the puzzle until everything works.