Search Unity

Problem with sync rigidbody Unet when spawn with velocity

Discussion in 'UNet' started by Duykk, May 29, 2019.

  1. Duykk

    Duykk

    Joined:
    May 27, 2016
    Posts:
    3
    Hello everyone, im making tank game and now i having a problem

    here is my fire bullet(shell) code:
    Code (CSharp):
    1.  
    2. Vector3 directionBullet;
    3.          void FixedUpdate()
    4.          {
    5.              if (tankManagerment.canFire)
    6.              {
    7.                      Fire();
    8.              }
    9.          }
    10.  
    11.          private void Fire()
    12.          {
    13.              CmdGenerateShell();
    14.          }
    15.  
    16.          [Command]
    17.          void CmdGenerateShell()
    18.          {
    19.              RpcAddShell();
    20.          }
    21.  
    22.          [ClientRpc]
    23.          void RpcAddShell()
    24.          {
    25.              GameObject shell = Instantiate(tankManagerment.currentBullet.prefab, m_FireTransform.position, m_FireTransform.rotation);
    26.              Vector3 velo = tankManagerment.currentBullet.velocity * directionBullet;
    27.              shell.GetComponent<Rigidbody>().velocity = velo;
    28.              NetworkServer.Spawn(shell);
    29.          }
    • With shell prefab had network identity (with localPlayerAuthority checked) and network tranform(with sync rigidbody mode).

    • So the problem is shell have been spawn successful in both client and server, but shell only move with velocity in server, in client it just spawn shell and do nothing. How to fix my problem? Thank you ^^ (sorry for my bad English).
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Don't call NetworkServer.Spawn inside a ClientRpc. You're also instantiating a separate shell object on every client which receives the ClientRpc. Don't instantiate objects you wish to spawn on the clients - you instantiate and spawn only on the server.

    The value of directionBullet is used in the ClientRpc, so independently on all clients, but you don't appear to be doing anything to ensure that the server and all clients have directionBullet set to the same value. So I would assume that the value is different, which means velo is different.

    I'm venturing a guess that no bullet is actually ever being spawned with your code as is. Just a bunch of independently instantiated bullet objects on each client, followed by a warning in the client's log about not being able to call NetworkServer.Spawn on clients. directionBullet is probably 0,0,0 on all clients except the client which called Fire(), so all clients except that client end up with a velo of 0,0,0. So the separate instantiated objects on all clients but the client which called Fire have no velocity.

    If you're this early on in your game, I'd seriously suggest dropping Unet. It was deprecated a year ago.
     
    Last edited: May 29, 2019
    Duykk likes this.
  3. Duykk

    Duykk

    Joined:
    May 27, 2016
    Posts:
    3
    Thank for your answer, so i tried put NetworkServer.Spawn on Command but it still doesn't work.
    About Unet, how many way can i use another way to make a multiplayer game without Unet? :(, i know it was deprecated but Unity still has not provided new methods until now.
     
  4. Duykk

    Duykk

    Joined:
    May 27, 2016
    Posts:
    3
    My mistake, i aware that directionBullet is local variable :p, i was fix my CmdGenerateShell() to CmdGenerateShell(Vector3 direction), pass directionBullet in and it work :D
     
    Joe-Censored likes this.