

The MoveShip script is very simple, in the FixedUpdate method we get the movement from the Horizontal Axis and set the ship velocity accordingly. Other scripts will be added later, but that’s it for now. In addition, the BoxCollider2D will be a trigger (Is Trigger set to true), since we don’t want collisions to affect the ship physics.įinally, we are going to add a MoveShip script, which will have a Speed parameter. Now, to handle movement and collisions, we need to add a RigidBody2D and a BoxCollider2D. Enable Client Authority on the NetworkTransform component. NetworkIdentity and NetworkTransform are the two components necessary for multiplayer features. Otherwise, if we’ve moved the ship in one screen, its position wouldn’t be updated in the other screens. The NetworkTransform component, by its turn, is responsible for updating the Ship position throughout the server and all the clients. In addition, since the ship will be controlled by the player, we are going to set the Local Player Authority check box for it. In order to a game object to be managed by the NetworkManager, we need to add the NetworkIdentity component to it. The figure below shows the Ship prefab components, which I will explain now. So, first of all, create a new GameObject called Ship and make it a prefab. Later on, we are going to add to it the ability to shoot bullets and receive damage. The first one we are going to create is the player ship.įor now, the ship will only move horizontally in the screen, with its position being updated by the NetworkManager. Now that we have the NetworkManager, we can start creating the game objects which will be managed by it. This is what we are going to do every time we need to test a multiplayer game. After doing that, you can enter Play Mode in the Unity Editor to run the second instance of the game. This will open a new window with the game. Then, you can generate and executable file and run it by clicking on File -> Build & Run. So, go to File -> Build Settings and add the Game scene to build. In order to build your game you need to add the Game scene to the build. The second instance can be run from the Unity Editor (in the Play Mode). In order to do so, you need to build your game and run the first instance from the generated executable file. However, you can not open two instances of the game in the Unity Editor. So, in order to test our game we are going to open two instances of it, one as the Host and another as the Client. The client communicates with the server, but do not execute any server only code. Then, other players can connect to this host by as clients (by selecting LAN Client). A host works as a client and a server at the same time. Unity multiplayer games work in the following way: first, a player starts a game as host (by selecting LAN Host). In this tutorial we are going to use the LAN Host and LAN Client options. The Network Manager HUD shows a simple HUD for the players to connect to a game.įor example, if you play the game now, you should see the following screen: This object will be responsible for managing for connecting different clients in a game and synchronizing the game objects among all clients. In order to have a multiplayer game, we need a GameObject with the NetworkManager and NetworkManagerHUD components, so let’s create one. Try running the game now and you should see the space background in the game. In the BackgroundImage we only need to set the Source Image, which will be the space one. This way the Canvas will appear in the background, and not in front of the other objects. Then, we are going to set its UI Scale Mode to Scale With Screen Size. In the BackgroundCanvas, we need to set its Render Mode to be Screen Space – Camera (remember to attach your Main Camera to it). We can do that by creating a new Image in the Hierarchy, and it will automatically create a new Canvas (rename it to BackgroundCanvas). The first thing we are going to do is creating a background canvas to show a background image.

#Unity web player games free code
You can download the tutorial source code files here. Go to the Asset Store ( Window > Asset Store) and download “Mirror”. In the Sprite Editor (shown below), you need to open the slice menu and click in the Slice button, with the slice type as automatic. In order to do that, we need to set the Sprite Mode as Multiple and open the Sprite Editor. However, some of those sprites are in spritesheets, such as the enemies spritesheets, and they need to be sliced. Unity Inspector will automatically import them to your project. In order to do that, create a folder called Sprites and copy all sprites to this folder. Before starting reading the tutorial, you need to create a new Unity project and import all sprites available through the source code.
