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

New to Unity, tell me if this is how I should do this

Discussion in 'Multiplayer' started by rennat, Feb 23, 2008.

  1. rennat

    rennat

    Joined:
    Feb 23, 2008
    Posts:
    11
    Hey everyone first post here :)

    -- about my Unity experience level
    Ok so I am at day 17 (or something) of my free trial but have been so impressed I'm going to buy the Indie version in the next few days. I've finished the 3D platformer tutorial, read pretty much every entry in the script reference, and built a maze game that generates levels from text strings supplied by a mysql database. I am a web developer. i use mostly PHP/MySQL with Javascript (Mootools is the framework of choice for this month.) I have had very basic C++ training and messed with Microsoft XNA using C#, although I still get lost very easily in anything .NET ;

    -- Ok so now on to what I am trying to do
    Purely as a way of learning how to use Unity I am trying to build a networked multiplayer Tron lightcycle game. I have the entire lightcycle part finished, and it is exactly what you'd expect (no AI as I intended it to be multiplayer from the start)

    -- My plan so far
    I have dissected some of the Networking examples from the unity site and from the Unify Wiki, and with my history in web development the first thing I thought of was to have the client and server logic built into the web player so the user decides whether to join an existing game or start a new one, and using PHP/MySQL to keep track of active servers.

    -- The real question
    What I am really hoping for is someone to point out either a really simple way of doing this all within Unity, or a link to a tutorial for networking noobs (from the ground up) or an example project with an ungodly amount of comments (history in linear PHP here, some O.O. concepts take a minute to click with me)

    -- An apology for this huge post
    Well I think I have successfully introduced myself in an inordinate fashion. Thanks for reading :)

    --Tanner
     
  2. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,306
    Hi Tanner,

    welcome!!!

    You might want to have a look at http://forum.unity3d.com/viewtopic.php?t=9517 - seems like we have a rather similar idea (I'm at V0.8 at the moment, currently developing V0.85) ;-) If you search the forums for TRaceON and TRON, you'll get pretty much the whole history (and there probably are some useful hints included)...

    It's kind of funny because I'm actually considering offering some parts of that game as tutorial later on - but my priority at the moment is on finishing and publishing it ;-)


    For the game listing and the user deciding which game to join, you could use the MasterServer - that handles pretty much all you'll ever need with almost no effort at all. I have a little link collection at http://forum.unity3d.com/viewtopic.php?t=9478#65917 and I think there's examples on how to do that. If you prefer doing it your own way (with the Web/PHP/MySQL approach), you could probably use the WWW classes, but you could probably still use some of the GUI-code from the examples as a starting point.

    I'm not sure how well Web players will work as servers, I remember having a bit of trouble with that, but I didn't follow up on it because I went the "standalone game server" route pretty much from the start, so don't take my word for it...

    Just a little hint: The "action networking" can get somewhat "involved" if you want to do it right and solid (I'm currently completely redesigning this in my own game). There's obviously different approaches, but I'm trying to make it work even with significant lag. Since the basic game logic is very simple, that *should* work, but it's not trivial...

    I think there's some links to an introduction to networking in the manual... um... see the end of
    http://unity3d.com/support/documentation/Components/net-HighLevelOverview.html

    What I've read and can highly recommend when you're serious about multiplayer is "Networked Virtual Environments - Design and Implementation", Sandeep Singhal and Michael Zyda.

    And of course: Feel free to ask any questions ;-)

    Sunny regards,
    Jashan
     
  3. rennat

    rennat

    Joined:
    Feb 23, 2008
    Posts:
    11
    Well I played your tron ( 0.8 ) and it's much better than what I have :) but that's ok im just making this to learn how to make it, I'll probably play yours for all my lightcycle needs.

    thanks for the quick response! and the lack of flaming as about 2 minutes after I posted I came across the Networking documentation (the online articles you linked to, I've read a lot of the local documentation about it but it seemed over my head) I'm working through them now, already much better off than I was.

    These will probably be answered before I finish reading these documents but a couple of questions so far...

    - Networking - Master server is a separate app (web or standalone) or is it part of each client? sort of fuzzy on how you actually get in touch with the master server.

    - Unity Scripting - coming from my background I have a habit of making one script for the level and passing it each specific level object and running all of the logic in one place. now obviously that is not the correct away of doing it but it made sense to me having all of the variables I might need available in one place. so how should the script be broken up? i guess without seeing it and fully understanding what my one script does that question can't be answered, although some guidelines/best practices as far as choosing what should be in each script would be wonderful.

    - Unity Scenes - done some tutorials and understand how to use LoadLevel(), just not sure the best way to implement it (other than when you beat level 1, load level 2 ...) for a different kind of game such as a tile-based game that generates the level from external data, is there a need to use loadlevel() other than for going from the start menu to the game to the gameover screen?

    wow, I am illustrating my "American-ness" by not being able to focus solely on one thing. I digressed from network variables to scenes. oh well, @mods : I'm new here please feel free to give me a lovetap if I offend you with semi-off-topic posts.

    -Tanner
     
  4. psx

    psx

    Joined:
    Aug 25, 2005
    Posts:
    80
    The master server is provided by the Unity folks. It's essentially a central location where when you create a server (with a unique gameName and some other vars) it gets registered with the master server. When you create a client with the same gameName, that client will see any open servers listed on the master server. IOW, it's the back-end for your game 'lobby'.

    It sounds like you have a central "GameController" script. This isn't a bad thing... IF it lives up to it's name. By that I mean, it shouldn't contain any input logic, or logic to control the player-character. It should only care about keeping score, starting/ending/restarting the game, etc.

    For the user/player related stuff.. I first create a PlayerCharacter game object. This would contain the character model (in your case, the lightcycle) and script to move the model around. Taking Tron as an example, you'd have accelerate, brake, and turn left or right. Importantly, there isn't any code here to take the actual input from the player (keyboard, mouse, etc) and direct the cycle. This gets handled in a separate script. The last thing to do with PlayerCharacter is to turn it into a prefab, and remove it from the scene.

    For actual control, I create a "PlayerController" script, attached to an empty gameObject in the scene. On Start, this script calls Network.Instantiate with the 'PlayerCharacter' prefab as a parameter, and stores a reference to PC's script in a variable. This script takes the keyboard/mouse input, and tells it's PC what to do. A rough example would be (in an update/fixedUpdate function)

    Code (csharp):
    1.  
    2.  
    3. var playerCharacter : PlayerCharacter;
    4.  
    5. function Update() {
    6.     var xmouse = Input.GetAxis("Mouse X");
    7.     if(xmouse) {
    8.          playerCharacter.accelerate(xmouse);
    9.     }
    10. }
    11.  
    Does that help at all? Depending on how you want things to work (over the network) you'd attach a NetworkView to your PlayerCharacter prefab and work with it's state sync and what object/component of the PC that it's supposed to sync.

    I feel like I got off on a bit of a tangent there, but it seems useful so I'll leave it be ;)

    As far as how to set things up client/server wise, If you're only ever planning on having 2 players, I'd structure it so that users first have the option of creating a game (server) or joining an existing game (client). So in any given game, one player is the server, and the second player is the client. Each player 'owns' their PlayerController and it's NetworkView. Using some of the ideas above, in your scripts you can use "Network.isClient" or "Network.isServer" to determine what to do. Any game logic should be handled by the server, and propagated to the client player (via NetworkView state-sync, or your own RPC calls).

    I use LoadLevel for exactly as you describe. GameStart/waiting scene (basically empty scene, with GUI stuff) and an InGame scene (where the game play happens). I haven't had a need for a GameOver scene yet but if I did, yes, I'd most likely create a scene for it. One thing that you're possibly going to stumble on is how to persist variables/state/whatever across scenes. There's a bunch of info on the forums here about that... do a search for "DontDestroyOnLoad".

    Random things I found useful:
    Search here and on the wiki for "singleton". Once that clicks, you'll see how you can use multiple singletons together to manage your game in a more OO fashion.

    This mp-maze demo is the closest thing I've found to a helpful example. The thing I didn't like about the networking demo on the Unity site was that it was hard to tell (initially) what was necessary to do what. The mp-maze is great simply because it only demonstrates one way to handle networking. Grab the demo and check out the thread here: http://forum.unity3d.com/viewtopic.php?t=8510&start=0&postdays=0&postorder=asc

    Phew! Hopefully some of that is useful! I'm still learning as well so take what you will :)

    psx
     
  5. rennat

    rennat

    Joined:
    Feb 23, 2008
    Posts:
    11
    Thanks!

    The Master server thing was stopping me because I was trying to figure out how to set up a new master server :roll:

    The networked game stuff is coming to me slowly. I'll keep rereading documentation and books, I'm sure it's about to click.

    -tanner