Search Unity

Calling method from instantiated network object

Discussion in 'Netcode for GameObjects' started by JayCE0, Jul 17, 2021.

  1. JayCE0

    JayCE0

    Joined:
    Sep 2, 2020
    Posts:
    4
    Hello!

    I'm new to networking and when I call .launchGrenade() method from inside of a ServerRpc, this method is correctly called on the instantiated gameObject and debug logs are also correct based on which client requested this call. But the problem is with .AddForce() function which seems only to trigger when host is the one requesting this call, in the case of a client, .AddForce() is not triggered. I'm really confused why is that..

    Code (CSharp):
    1.    
    2. [ServerRpc]
    3.     private void SpawnGrenadeServerRpc()
    4.     {
    5.         // Instantiate grenade network object
    6.         NetworkObject grenade = Instantiate(Grenade, mortarBarrel.position, Quaternion.identity);
    7.         // Set ownership to player for game statistics
    8.         grenade.SpawnWithOwnership(OwnerClientId);
    9.         // Launch grenade
    10.         grenade.GetComponent<Grenade>().launchGrenade(mortarBarrel.forward * grenadeLaunchSpeed);
    11.     }
    12.  

    Code (CSharp):
    1. public class Grenade : MonoBehaviour
    2. {
    3.     [SerializeField] private Rigidbody RB;
    4.  
    5.     public void launchGrenade(Vector3 force)
    6.     {
    7.         Debug.Log(force);
    8.         Debug.Log(gameObject.GetComponent<NetworkObject>().OwnerClientId);
    9.         RB.AddForce(force);
    10.     }
    11. }

    Thanks a lot for any help.
     
    Last edited: Jul 18, 2021
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    Do you maybe have a `NetworkTransform` attached to this object? In that case the position on the clients will always be based on the owners position and it will ignore the rigidbody's position.
     
  3. JayCE0

    JayCE0

    Joined:
    Sep 2, 2020
    Posts:
    4
    Yes I do, also I've recently read about problem with rigidbodies and network transforms, yet it works completely fine with me (when I use .AddForce() at rigidbody, NetworkTransform will update it's new position to all clients, which I don't know if I understood the problem correctly, because I thought that NetworkTransform and Rigidbody operations are not synced).

    The way I was able to fix my problem was to Instatiate() grenade NetworkObject with given rotation, and from inside of a Grenade script just call .AddForce(transform.forward) and all works fine now.

    Code (CSharp):
    1.     [ServerRpc]
    2.     private void SpawnGrenadeServerRpc()
    3.     {
    4.         // Instantiate grenade network object
    5.         NetworkObject grenade = Instantiate(Grenade, mortarBarrel.position, mortarBarrel.rotation);
    6.         // Set ownership to player for game statistics
    7.         grenade.SpawnWithOwnership(OwnerClientId);
    8.     }
    Code (CSharp):
    1. public class Grenade : MonoBehaviour
    2. {
    3.     [SerializeField] private Rigidbody RB;
    4.     [SerializeField] private float launchSpeed = 1000f;
    5.  
    6.     private void Start()
    7.     {
    8.         RB.AddForce(transform.forward * launchSpeed);
    9.     }
    10. }
     
    luke-unity likes this.