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

Strange bug with bullet spawning

Discussion in 'Multiplayer' started by UnbreakableOne, Oct 28, 2015.

  1. UnbreakableOne

    UnbreakableOne

    Joined:
    Nov 16, 2013
    Posts:
    168
    Hi,

    I have a strange bug with my bullets. When a client fires, I can see the bullet instantiation, initializes and moving on the host machine but bullet instantiates but doesn't move on the client machines.

    I fire bullets with calling a Command on the server with this code

    Code (csharp):
    1.  
    2.   public void CmdFireProjectile(float Speed, int Damage, int RicochetTimes, float aimInDegrees, Vector2 velocity, float timeToLiveModifier)
    3.   {
    4.   // we instantiate the projectile at the projectileFireLocation's position.
    5.   GameObject projectile = (GameObject)Instantiate(
    6.   ProjectilePrefab.gameObject,
    7.   transform.position, /*ProjectileFireLocation.position*/
    8.   transform.rotation
    9.   );
    10.  
    11.   projectile.GetComponent<BaseProjectile>().Owner = gameObject;
    12.   projectile.GetComponent<BaseProjectile>()._speed = Speed;
    13.   projectile.GetComponent<BaseProjectile>()._damage = Damage;
    14.   projectile.GetComponent<BaseProjectile>().remainingRichocets = RicochetTimes;
    15.   projectile.GetComponent<BaseProjectile>().Direction = new Vector2(Mathf.Cos(Mathf.Deg2Rad * aimInDegrees), Mathf.Sin(Mathf.Deg2Rad * aimInDegrees));
    16.   projectile.GetComponent<BaseProjectile>().InitialVelocity = velocity;
    17.   projectile.GetComponent<BaseProjectile>().TimeToLive = timeToLiveModifier;
    18.   projectile.GetComponent<BaseProjectile>().lineRenderer.SetColors(ColorTint, new Color(ColorTint.r, ColorTint.g, ColorTint.b, 0));
    19.  
    20.   projectile.GetComponent<BaseProjectile>().lastPosition = transform.position;
    21.  
    22.   projectile.gameObject.name += "_" + "Player"; // +  gameObject.GetComponentInParent<PlayerController>().PlayerId;
    23.  
    24.   NetworkServer.Spawn(projectile);
    25.  
    26.   }
    27.  
    28.  
    What can cause this behaviour? What am I missing here?

    Thanks
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    The first thing you should do is cache that GetComponent.

    Code (csharp):
    1.  
    2.   public void CmdFireProjectile(float Speed, int Damage, int RicochetTimes, float aimInDegrees, Vector2 velocity, float timeToLiveModifier)
    3.   {
    4.   // we instantiate the projectile at the projectileFireLocation's position.
    5.   GameObject projectile = (GameObject)Instantiate(
    6.   ProjectilePrefab.gameObject,
    7.   transform.position, /*ProjectileFireLocation.position*/
    8.   transform.rotation
    9.   );
    10.  
    11.   // cache it. call GetComponent once.
    12.   bp = projectile.GetComponent<BaseProjectile>();
    13.  
    14.   bp.Owner = gameObject;
    15.   bp._speed = Speed;
    16.   bp._damage = Damage;
    17.   bp.remainingRichocets = RicochetTimes;
    18.   bp.Direction = new Vector2(Mathf.Cos(Mathf.Deg2Rad * aimInDegrees), Mathf.Sin(Mathf.Deg2Rad * aimInDegrees));
    19.   bp.InitialVelocity = velocity;
    20.   bp.TimeToLive = timeToLiveModifier;
    21.   bp.lineRenderer.SetColors(ColorTint, new Color(ColorTint.r, ColorTint.g, ColorTint.b, 0));
    22.  
    23.   bp.lastPosition = transform.position;
    24.  
    25.   projectile.gameObject.name += "_" + "Player"; // +  gameObject.GetComponentInParent<PlayerController>().PlayerId;
    26.  
    27.   NetworkServer.Spawn(projectile);
    28.  
    29.   }
    30.  
    31.  
     
  3. UnbreakableOne

    UnbreakableOne

    Joined:
    Nov 16, 2013
    Posts:
    168
    Thanks but that's not the problem here. I'm trying to "make it work" first so I avoid any optimizations for now.
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Are you using BaseProjectile.InitialVelocity in some other method to move it? If so I would debug if that method runs and if InitialVelocity is getting passed to the client properly.
     
  5. UnbreakableOne

    UnbreakableOne

    Joined:
    Nov 16, 2013
    Posts:
    168
    InitialVelocity is just player's speed that I add to bullet initially.
     
  6. UnbreakableOne

    UnbreakableOne

    Joined:
    Nov 16, 2013
    Posts:
    168
    I've investigated the problem more. Problem is all values are zero, despite being initialized by Command.

    I've studied Invaders sample and it's doing the same thing.

    I instantiate the bullet, call Initialize on it and then move it with Translate. So why on the client side, all the values of BaseProjectile are zero?

    I thought I have to sync them with SyncVars but apparently not.

    Any guess would help at this stage.
     
  7. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    is there a networktransform on the bullet?
     
  8. UnbreakableOne

    UnbreakableOne

    Joined:
    Nov 16, 2013
    Posts:
    168
    Hi Mr. Riley, big fan!

    Yes there is. It's Network Send Rate is set to zero, sync mode is set to Rigidbody 2D and Rotation to none.
    (I've tried to mimic what you've done in Unite 2014 and Invaders sample project)

    I'm trying to avoid Network Transform as my bullets don't have a rigidbody2D and as I understood, setting sync mode to Transform, it won't do interpolation.

    Problem is the initialized values from Command are not present in the clients other than the host.

    Cheers.
     
    Last edited: Oct 28, 2015
  9. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    The message that causes objects to be spawned on the client does not include velocity. Adding a NetworkTransform adds that data to the spawn message. If you dont use NetworkTransform, then you will have to network velocity some other way.

    You could make a SyncVar with the velocity in it and set it on the client in OnStartClient.
     
  10. UnbreakableOne

    UnbreakableOne

    Joined:
    Nov 16, 2013
    Posts:
    168
    Thanks


    Problem is the initialized values from Command are not present in the clients other than the host
     
  11. UnbreakableOne

    UnbreakableOne

    Joined:
    Nov 16, 2013
    Posts:
    168
    In other words, the bullet I instantiate and init in my Command appear on other clients but without the initialized values. Not just velocity is missing but everything else as well like TimeToLive, etc.
     
  12. NahuGuzmy

    NahuGuzmy

    Joined:
    Aug 15, 2015
    Posts:
    14
    Same problem here... Waiting for a solution
     
  13. Dhezyr

    Dhezyr

    Joined:
    Feb 10, 2014
    Posts:
    5
    Initialized values in the command missing in the client.

    Code (CSharp):
    1.  
    2. [Command]
    3.         void CmdTransmitUserInput(string userInput)
    4.         {
    5.             GameObject text = (GameObject)Instantiate(chatText, transform.position, transform.rotation);
    6.             var chatTextInstance = text.GetComponent<ChatText>();
    7.             chatTextInstance.text = userInput;
    8.             chatTextInstance.playerTransform = transform;
    9.  
    10.             NetworkServer.Spawn(text);
    11.          
    12.         }
    13.  
    playerTransform is not using syncvar and is null on the client. Throws exception.
    text property is using syncvar and its ok.

    Should we use syncvar for every property in networkspawned objects?
     
  14. UnbreakableOne

    UnbreakableOne

    Joined:
    Nov 16, 2013
    Posts:
    168
    From what I understood so far, yes.
     
    Dhezyr likes this.
  15. UnbreakableOne

    UnbreakableOne

    Joined:
    Nov 16, 2013
    Posts:
    168
    Is there any other note like this on using uNet? And what should I read to know more of these beforehand?