Search Unity

Knockback with Networking not syncing

Discussion in 'Multiplayer' started by TnoffApps, May 14, 2021.

  1. TnoffApps

    TnoffApps

    Joined:
    Dec 5, 2020
    Posts:
    2
    I am trying to implement knockback in an online game. I am using photon, and when I hit a player, they go backward, but then sync to their original position. Here is the code I am using:
    Code (CSharp):
    1.     [PunRPC]
    2.     void OnCollisionEnter(Collision col)
    3.     {
    4.          
    5.         if(col.gameObject.tag == "Player"){
    6.             Vector3 pushDirection = col.transform.position - transform.position;
    7.  
    8.             pushDirection = pushDirection.normalized;
    9.  
    10.             col.gameObject.GetComponent<Rigidbody>().AddForce(pushDirection * force * 300);
    11.  
    12.                  
    13.        
    14.     }
    15.  
    16.  
    17. }
    Any ideas on how to fix this?
     
  2. toddkc

    toddkc

    Joined:
    Nov 20, 2016
    Posts:
    207
    You need to think about ownership. Each player owns their own avatar and it can only be moved by forces on their client. Using OnCollisionEnter as an RPC is also not a good idea, although the way you are doing it won't call it as an RPC. Do you know how PUN RPCs work?

    Essentially what you need to do is have the pusher (Player B) tell the pushee (Player A) that they were pushed using an RPC and then have them do the movement on their (Player A) machine, which will in turn sync it to all others as they own their avatar. This of course gets even more complex if you are doing any sort of MasterClient authority where players have to ask for permission to move.

    Player interaction/knockback is one of those network things that sounds so simple, but actually getting it to work properly can be incredibly difficutl.
     
  3. TnoffApps

    TnoffApps

    Joined:
    Dec 5, 2020
    Posts:
    2
    I got it working somewhat, but only the host is being knocked back. Here is my code.

    Code (CSharp):
    1.     void OnCollisionEnter(Collision col)
    2.     {
    3.        
    4.         if(col.gameObject.tag == "Player"){
    5.             col2 = col;
    6.             photonView.RPC("knockback",PhotonTargets.Others);
    7.         }
    8.     }
    9.     [PunRPC]
    10.     void knockback(){
    11.             Vector3 pushDirection = col2.transform.position - transform.position;
    12.             Vector3 pushDirection2 = col2.transform.position - transform.position;
    13.  
    14.             pushDirection = pushDirection.normalized;
    15.  
    16.             col2.gameObject.GetComponent<Rigidbody>().AddForce(pushDirection * force);
    17.  
    18.             Debug.Log("Hitting Player");
    19.  
    20.             pushDirection2 = -pushDirection.normalized;
    21.  
    22.             GetComponent<Rigidbody>().AddForce(pushDirection2 * force * 5);
    23.  
    24.     }
    Why isn't the client being knocked back? It seems like both players should recognize a collision and then both send eachother back.