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

2D Multiplayer bullets not spawning at proper position

Discussion in '2D' started by ebourque, Sep 8, 2016.

  1. ebourque

    ebourque

    Joined:
    Jul 16, 2016
    Posts:
    2
    Hello! Working on yet another 2D space game. I think this thread could help allot of people if solved.

    Been searching for a while, no luck as of yet.

    My problem is that the bullets on the client side (server side is fine) are spawning as if the spawn position sent by the server is not taking into account the position that the ship will be in when the client receives the command.

    In other words, I need a way to spawn the bullets a bit further away from my ship when my ship is moving fast.

    I tried simply adding the velocity of my ship to the spawn position but it's always either too far or too close.

    Using this CMD function:

    Code (csharp):
    1.  
    2. [Command]
    3.     void Cmd_Fire()
    4.     {      
    5.         Vector3 initial = GetComponent<Rigidbody2D>().velocity * initialVelocityMultiplier;
    6.  
    7.         foreach (var sp in bulletSpawns)
    8.         {
    9.             // Create the Bullet from the Bullet Prefab
    10.             var bullet = (GameObject)Instantiate(
    11.                             bulletPrefab,
    12.                             sp.position + initial,
    13.                             sp.rotation);
    14.        
    15.             // Spawn the bullet on the Clients
    16.             NetworkServer.Spawn(bullet);
    17.  
    18.             Destroy(bullet, 3.0f);
    19.         }
    20.     }
    21.  
    The "bulletSpawns" are hardpoints. Using 2 at the moment.

    Here is a video 20s of what this looks like (notice the bullets are being eaten by the ship if the speed is too high):

     
    Last edited: Sep 11, 2016
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Hey there, welcome to the forums. Please use Code Tags when pasting code snippets.

    As far as I know you shouldn't need to spawn the bullet with a predictive additional position.

    The way I have handled bullet spawning in the past is to supply the projectile script (the one that starts the projectile's movement) with the current velocity of the ship. That way the projectile can add the ship's current velocity into its own velocity when it starts. Maybe give your projectile script a "Launch" function or something that takes a Vector3 velocity as a parameter to add into it's own speed before you add the force.

    You can also prevent the self-collision by using Physics2D.IgnoreCollision to prevent collision between the bullet and the ship that fired it.
     
  3. ebourque

    ebourque

    Joined:
    Jul 16, 2016
    Posts:
    2
    Hi JS,

    Thanks it looks cleaner now. The bullet stills spawns at around 5-10 degrees off center when the ship is doing a speeding turn and still spawns too far back when speed is high.

    I think I might not have a choice but to add a predictive additional position here.

    Anyway to get av. latency (or most recent) between server and client? Could I use that instead of Time.DeltaTime to get a good estimation of where the ship will be when it receives the command?

    Thanks
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Forgive my ignorance, as I'm only very recently diving into the networking side of Unity.

    With some quick research, I've found you can use either "GetCurrentRTT" which gets you the round-trip latency, or "GetAveragePing" which will get ping from server to client i believe.

    Could you perhaps have the bullet spawn through a ClientRPC from the server? So the client would call the Fire command which would in turn call a ClientRPC, and then the client would handle the spawning accurately? I'm not sure how the positioning would sync up across clients though.
     
    ebourque likes this.