Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question I can't apply force to objects with netcode

Discussion in 'Netcode for GameObjects' started by sexyeys01, Sep 1, 2023.

  1. sexyeys01

    sexyeys01

    Joined:
    May 7, 2023
    Posts:
    3
    I'm making a simple car game using Unity netcode. I am using Network Rigidbody. However, I cannot push the vehicles I hit. How can i solve this. Sorry for my english :(





     
  2. Nyphur

    Nyphur

    Joined:
    Jan 29, 2016
    Posts:
    69
    NetworkRigidbody makes the rigidbody of objects you don't have ownership of kinematic, so they won't react to collisions. This is the correct response, because the owner of the object is responsible for its own physics.

    Typically you would have to do this in code by having the owner of the car add the force of the collision using Rigidbody.AddForce. For example, you could detect the collision on the client that does the crashing, send the collision force and vector to the server via a serverrpc, and the server calls a clientrpc that tells whoever owns that car to apply that force.
     
  3. sexyeys01

    sexyeys01

    Joined:
    May 7, 2023
    Posts:
    3


    i have a code snippet like this


    Code (CSharp):
    1. public class CarCollider : NetworkBehaviour
    2. {
    3.     public Rigidbody rb;
    4.  
    5.     [ServerRpc]
    6.     void HandleCollisionServerRpc(Vector3 forceDirection, float forceMagnitude)
    7.     {
    8.         ApplyForceClientRpc(forceDirection, forceMagnitude);
    9.     }
    10.  
    11.     [ClientRpc]
    12.     void ApplyForceClientRpc( Vector3 forceDirection, float forceMagnitude)
    13.     {
    14.         rb.AddForce(forceDirection * forceMagnitude, ForceMode.Impulse);
    15.     }
    16.  
    17.     void OnCollisionEnter(Collision collision)
    18.     {
    19.         if (!IsLocalPlayer)
    20.         {
    21.             return;
    22.         }
    23.  
    24.         if (collision.gameObject.CompareTag("Player"))
    25.         {
    26.             GameObject otherGO = collision.gameObject;
    27.             Vector3 forceDirection = collision.contacts[0].normal.normalized;
    28.             forceDirection.y = 0;
    29.             float forceMagnitude = 15;
    30.             HandleCollisionServerRpc(forceDirection, forceMagnitude);
    31.         }
    32.     }
    33.  
    34. }
    However it still doesn't work.
     
  4. Nyphur

    Nyphur

    Joined:
    Jan 29, 2016
    Posts:
    69
    That does look like it should work, is OnCollisionEnter being called correctly? Unity has some physics interactions disabled for kinematic rigidbodies. It also looks like the physics bump is being applied to the car that does the crashing, you need to call the serverRPC on the other car.
     
  5. JethRow

    JethRow

    Joined:
    Sep 22, 2021
    Posts:
    10
    You need to tell the server which rigidbody to add the force to, one way of doing it is getting network object of collider hit, probably in his parents so note that, then you need to send that networkID through the RPC, because as is right now, you are adding force to the rigidbody that ur controlling, and not the one you hit
     
  6. Nyphur

    Nyphur

    Joined:
    Jan 29, 2016
    Posts:
    69
    Technically in this instance you can get away with not sending the network object ID or any of that. What would happen is that rigidbody.applyforce would be called on every client, but would do nothing on everyone's client except the owner because the rigidbody would be kinematic for everyone else.

    But the main point is correct, you need to invoke the ServerRpc on the car that gets hit, currently it's happening on the car that is driving.
     
  7. sexyeys01

    sexyeys01

    Joined:
    May 7, 2023
    Posts:
    3
    As a result, no action takes place. because rigid bodies are kinematic. That's why no one can do each other. How do I solve it?
     
  8. JethRow

    JethRow

    Joined:
    Sep 22, 2021
    Posts:
    10
    By executing ClientRpc, telling which client should apply force to its own rb, u can send vector3 for direction and float for force, also ulong of networkID of a client or clientID