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

Sync Particle System?

Discussion in 'Multiplayer' started by MJ-Z, Jul 7, 2015.

  1. MJ-Z

    MJ-Z

    Joined:
    May 29, 2015
    Posts:
    6
    I've been playing around with the new Unet sample projects and the presentation demo.
    Most things seem to be working as advertised for now, I would definitely like to see more examples and hopefully tutorials and live learning sessions down the road to help me better learn and understand what's going on.

    Anyway, my question is how to sync a player's particle system so it's visible to the other players?
    Example is the space shooter demo from the unity presentation, the 2D shooter game and the tank game.
    When a player thrusts his airplane he can see the particle system but other players can't.

    In all these 3 sample projects, all code related to synchronizing the particle systems is incomplete and all commented out giving me no example of a working sample. Tried to tinker around with RPC and state synchronization with no luck.

    Any help is much appreciated.
     
  2. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Hey did you figure it out? I'm struggling with the same here, and I'm halfway there atm. Clients see the host's thrusters, but the host doesn't see the clients.
     
  3. MJ-Z

    MJ-Z

    Joined:
    May 29, 2015
    Posts:
    6
    Haha Chris, I was playing around with the "Thrusting" bool to make the PS visible for others and got it to work the opposite of what you mentioned (Host can see them but not clients), so no go yet.
    It seems to me like the client is not able to control another ship's Thrusters because they are part of the player which can only be controlled by the authoritative player. Someone mentioned we should look at making them local and not necessarily in sync which might work, I'll give it a try and let you know

    I also opened a question wasn't sure which one to use at first, just a reference for others like me scanning for a solution:
    http://answers.unity3d.com/answers/1005529/view.html
     
  4. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    I'm tested with three game windows, and the clients can see the host's thrusters, but the host doesn't see the clients, nor do the other clients see each others thrusters. Only host's thrusters are visible. I've been reading and in a few places, like here and here, it appears to be a bug from what I've read. Or its not, since not even the clients can see other client's thrusters. Damn why do I always pick the harder stuff to play with. :D

    ClientRpc should send out to all clients, including the host, since the host is a client too. I'm downloading 5.1.1p2 patch release, but I doubt that will fix the issue.
     
  5. MJ-Z

    MJ-Z

    Joined:
    May 29, 2015
    Posts:
    6
    yeah I'm starting to think this is a bug too, no one can control the particle system attached to a player prefab except the authoritative player. and you would think any particle system that's attached to a player object should be shared by default like transform but they're not. I'm still scratching the surface on how to use Unet really so trying to figure this out is a lengthy trial and error process for me
     
  6. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Check out my thread. wasstraat65 has been helpful, and his idea might work for you. I've also figured out a solution for my case, in a different way, tested once with 3 players and everything worked. I didn't have time to do more testing or post my solution, but if you look at my thread and here in my other thread, you might be able to piece it together. :)

    I'll try to post my solution tomorrow, I was out all day, and working on some asset updates, so I didn't get a chance to play with it yet. :D
     
  7. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
  8. MJ-Z

    MJ-Z

    Joined:
    May 29, 2015
    Posts:
    6
    Thanks for the tip, I got it to work perfectly, here is my code...
    Code (csharp):
    1.  
    2.     [SyncVar]
    3.     float thrusting;
    4. ...
    5.     [Command]
    6.     void CmdThrusters(int newThrust, int newSpin)
    7.     {
    8.         RpcThrusters(newThrust, newSpin);
    9.     }
    10.     [ClientRpc]
    11.     void RpcThrusters(int newThrust, int newSpin)
    12.     {
    13.         if(isLocalPlayer)
    14.             return;
    15.         DoThrust(newThrust, newSpin);
    16.     }
    17.  
    18.     void DoThrust(int newThrust, int newSpin)
    19.     {
    20.         if(isLocalPlayer)
    21.             CmdThrusters(newThrust, newSpin);
    22.  
    23.         // turn thrusters on and off
    24.         if (newThrust == 0)
    25.         {
    26.             thruster1.Stop();
    27.             thruster2.Stop();
    28.         }
    29.         else
    30.         {
    31.             Quaternion rot;
    32.             if (newThrust > 0)
    33.             {
    34.                 rot = Quaternion.Euler(0,0,180);
    35.             }
    36.             else
    37.             {
    38.                 rot = Quaternion.Euler(0,0,0);
    39.             }
    40.             thruster1.transform.localRotation = rot;
    41.             thruster1.Play();
    42.             thruster2.transform.localRotation = rot;
    43.             thruster2.Play();
    44.         }
    45.         if(!isLocalPlayer)
    46.             return;
    47.  
    48.         // apply new values
    49.         this.thrusting = newThrust;
    50.         this.spin = newSpin;
    51.     }
    52.  
     
    Last edited: Jul 15, 2015
    RkTheGuy, Mgayar and ChrisSch like this.
  9. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Great! :D
    We've arrived at a similar script. :D

    Here's my post with the code.
     
  10. ChrisSch

    ChrisSch

    Joined:
    Feb 15, 2013
    Posts:
    763
    Hey are you initially calling the "DoThrust()" from update?
     
  11. MJ-Z

    MJ-Z

    Joined:
    May 29, 2015
    Posts:
    6
    yes, the original script except I added the return if !isLocalPlayer.
    Here is the complete Controls.cs script....

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class Controls : NetworkBehaviour {
    5.  
    6.     [SyncVar]
    7.     float thrusting;
    8.     float spin;
    9.  
    10.     int oldThrust;
    11.     int oldSpin;
    12.  
    13.  
    14.     float rotateSpeed = 200f;
    15.     float acceleration = 8f;
    16.     float bulletSpeed = 12f;
    17.  
    18.     public GameObject bulletPrefab;
    19.  
    20.     public ParticleSystem thruster1;
    21.     public ParticleSystem thruster2;
    22.  
    23.     void FixedUpdate()
    24.     {
    25.         if (!isLocalPlayer)
    26.             return;
    27.  
    28.         // update thrust
    29.         if (thrusting != 0)
    30.         {
    31.             Vector3 thrustVec = transform.right * thrusting * acceleration;
    32.             GetComponent<Rigidbody2D>().AddForce(thrustVec);
    33.         }
    34.      
    35.         // update rotation
    36.         float rotate = spin * rotateSpeed;
    37.         GetComponent<Rigidbody2D>().angularVelocity = rotate;
    38.     }
    39.  
    40.     void Update ()
    41.     {
    42.         if (!isLocalPlayer)
    43.             return;
    44.  
    45.         // movement
    46.         int newSpin = 0;
    47.         if (Input.GetKey(KeyCode.LeftArrow))
    48.         {
    49.             newSpin += 1;
    50.         }
    51.         if (Input.GetKey(KeyCode.RightArrow))
    52.         {
    53.             newSpin -= 1;
    54.         }
    55.      
    56.         int newThrust = 0;
    57.         if (Input.GetKey(KeyCode.UpArrow))
    58.         {
    59.             newThrust += 1;
    60.         }
    61.         if (Input.GetKey(KeyCode.DownArrow))
    62.         {
    63.             newThrust -= 1;
    64.         }      
    65.      
    66.         if (oldThrust != newThrust || oldSpin != newSpin)
    67.         {
    68.             DoThrust (newThrust, newSpin);
    69.             oldThrust = newThrust;
    70.             oldSpin = newSpin;
    71.         }
    72.  
    73.         // fire
    74.         if (Input.GetKeyDown(KeyCode.Space))
    75.         {
    76.             SpaceManager.Reset();
    77.             //Debug.Log("CmdDoFire");
    78.             CmdDoFire(transform.position, GetComponent<Rigidbody2D>().rotation);
    79.         }
    80.      
    81.         // center camera..
    82.         Vector3 pos = transform.position;
    83.         pos.z = -10;
    84.         Camera.main.transform.position = pos;
    85.     }
    86.  
    87.     [Command]
    88.     void CmdThrusters(int newThrust, int newSpin)
    89.     {
    90.         RpcThrusters(newThrust, newSpin);
    91.     }
    92.     [ClientRpc]
    93.     void RpcThrusters(int newThrust, int newSpin)
    94.     {
    95.         if(isLocalPlayer)
    96.             return;
    97.         DoThrust(newThrust, newSpin);
    98.     }
    99.  
    100.     void DoThrust(int newThrust, int newSpin)
    101.     {
    102.         if(isLocalPlayer)
    103.             CmdThrusters(newThrust, newSpin);
    104.  
    105.         // turn thrusters on and off
    106.         if (newThrust == 0)
    107.         {
    108.             thruster1.Stop();
    109.             thruster2.Stop();
    110.         }
    111.         else
    112.         {
    113.             Quaternion rot;
    114.             if (newThrust > 0)
    115.             {
    116.                 rot = Quaternion.Euler(0,0,180);
    117.             }
    118.             else
    119.             {
    120.                 rot = Quaternion.Euler(0,0,0);
    121.             }
    122.             thruster1.transform.localRotation = rot;
    123.             thruster1.Play();
    124.             thruster2.transform.localRotation = rot;
    125.             thruster2.Play();
    126.         }
    127.         if(!isLocalPlayer)
    128.             return;
    129.  
    130.         // apply new values
    131.         this.thrusting = newThrust;
    132.         this.spin = newSpin;
    133.     }
    134.  
    135.     [Command]
    136.     void CmdDoFire(Vector3 pos, float rotation)
    137.     {
    138.         Debug.Log("CmdDoFire invoke:" + SpaceManager.Now());
    139.         GameObject bullet = (GameObject)Instantiate(
    140.             bulletPrefab,
    141.             pos + transform.right,
    142.             Quaternion.Euler(0,0,rotation));
    143.          
    144.         var bullet2D = bullet.GetComponent<Rigidbody2D>();
    145.         bullet2D.velocity = bullet2D.transform.right * bulletSpeed;
    146.         Destroy(bullet, 2.0f);
    147.  
    148.         NetworkServer.Spawn(bullet);
    149.     }
    150. }
    151.  
     
    Last edited: Jul 15, 2015