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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Using Rigidbody.AddForce with UNet spawned prefab via Network.Spawn

Discussion in 'UNet' started by leehall02, Oct 1, 2015.

  1. leehall02

    leehall02

    Joined:
    Mar 4, 2015
    Posts:
    2
    Hi,
    I'm sure this must have been asked previously...
    I'm currently trying to apply force to a Rigidbody component of a prefab thats getting spawned via Network.Spawn however it doesnt appear to be working.

    My Prefab has a Network Identity and Network Transform components. I have tried setting the Network Transform Sync mode to Sync Rigidbody 3D
    and registerd the component via the NetworkManager SpawnInfo editor UI component

    On my player I have a script which notifies the server to spawn my prefab if the player clicks the mouse button as per the code below.
    Am I missing something?

    TIA,

    Lee

    Code (CSharp):
    1.  
    2.   void Update ()
    3.   {
    4.   CheckIsAttacking();
    5.   }
    6.  
    7.   private void CheckIsAttacking()
    8.   {
    9.   if (!isLocalPlayer)
    10.   return;
    11.  
    12.   if(Input.GetKeyUp(KeyCode.Mouse0))
    13.   {
    14.  
    15.   SpawnPrefab();
    16.   }
    17.   }
    18.   private void SpawnPrefab()
    19.   {
    20.   CmdSpawnPrefab();
    21.   }
    22.  
    23.  
    24.   [Command]
    25.   private void CmdSpawnPrefab()
    26.   {
    27.   GameObject spawnObj = (GameObject)Instantiate(spawnPrefab, transform.TransformPoint(0, 0, 0.5f), Quaternion.identity);
    28.   spawnObj.GetComponent<Rigidbody>().AddForce(transform.forward * 500f);
    29.   NetworkServer.Spawn(spawnObj);
    30.   }
     
  2. p627

    p627

    Joined:
    Apr 12, 2014
    Posts:
    23
    Code (CSharp):
    1. Rigidbody shot = Instantiate(bulletPrefab.GetComponent<Rigidbody>(), barrelOut.position, barrelOut.rotation) as Rigidbody;
    2. shot.velocity = barrelOut.forward * 250f;
    3. NetworkServer.Spawn(shot.gameObject);
    and add an NetworkTransform with 0 send rate.. that all
     
  3. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    physics forces are applied asynchronously in unity. In your code when NetworkServer.Spawn() is called, the object's physics properties such as velocity have not been effected by the force yet.

    To work around this you can spawn the object in a later frame, or set properties such as velocity directly.
     
  4. Nosfera

    Nosfera

    Joined:
    Sep 26, 2014
    Posts:
    12

    How's that?
    What do you mean "in a later frame, or set properties such as velocity directly"?

    I have the same issue in here =(
    Did you figure it out already??