Search Unity

Retrieving Number Of Players And Using It To Activate Appropriate Player Game Object

Discussion in 'Multiplayer' started by novica89, Apr 13, 2019.

  1. novica89

    novica89

    Joined:
    Feb 8, 2019
    Posts:
    9
    Since Unity networking stuff is pretty much deprecated and I'm using 2018.3.9f1 version of Unity currently (in which some of the "old" networking things have been completely removed now), I'm losing my mind on how to hook onto event when a player joins a multiplayer game (using the HLAPI) so that I can take the player count and using that integer actually "activate" a desired character for the player who joined. Since I am making a 2D game which should have 2 players in order to start it, I need to have this way of hooking onto an event when a player joins in so that I can trigger my own logic after that which would enable his own character.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Easiest way to do this in my opinion is to separate the Player GameObject from the character object. Have a script on the Player object which in Start finds a singleton on the server and basically announces itself. The singleton keeps track of how many players there are, so will know which player number this is. Then you spawn the correct character object for the player. Doing it this way doesn't require listening for any events.

    YMMV
     
  3. novica89

    novica89

    Joined:
    Feb 8, 2019
    Posts:
    9
    Thank you for the reply Joe. Much appreciated. I have managed after a lot of fiddling around to create this functionality and spawn a correct character (I used approach similar to yours). I'd need one more thing, if you are aware of where I could look for help regarding this. Unity's Network Transform does terrible job synchronizing dynamic Rigidbody2D over the network for the client player. The goal of my game should be: 2 players with dynamic Rigidbody2D components (since I need gravity and physics) and one ball object that would interact with each player when the player jumps and hits the ball with his head. So the ball also has dynamic Rigidbody2D. I have tried numerous ways and options to make it synchronize properly, but it just wont. When the ball collides with a client player it kinda sticks to the player, goes around him like a magnet, sometimes it shoots out, very rarely it calculates the collision properly (if ever). I've also used Smooth Sync package over at Unity store (premium package that says it even does dynamic Rigidbody synchronization), but it is also not working properly.
    I would appreciate any pointers on this and / or any online tutorials on the subjects, whether in written or video form. Same for books.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The proper settings for the NetworkTransform component, and what its fields even mean, are notoriously poorly documented. When I was using Unet I ended up giving up on it and just writing my own using ClientRPCs over an unreliable channel to fit my specific needs. That would be my suggestion.

    Sending position, rotation, and velocity in an RPC is pretty trivial. The only tricky part is implementing some smooth interpolation on the receiving end.

    (eventually though I ended up giving up on Unet entirely, I'd suggest you do the same unless your project is doing well on Unet and you're close to completion)
     
    novica89 likes this.
  5. novica89

    novica89

    Joined:
    Feb 8, 2019
    Posts:
    9
    Thank you for the suggestion :) Any idea of where I could find an example of this online (tutorial, book, anything really) ? I'm currently learning Unity and I do know commands, rpc's etc, but putting it all together and the concept behind it for my case is a bit difficult to grasp on right now as I probably don't currently know ALL the things that I should to make it happen :)
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I'm not aware of a tutorial, but you might find some older threads on it.
     
    novica89 likes this.
  7. novica89

    novica89

    Joined:
    Feb 8, 2019
    Posts:
    9
    Thank you for all of your help Joe :) I appreciate it!