User Tools

Site Tools


public:t-vien-14-1:lab_8_materials

Lab 8: Networking

Goal

The goal of this lab is to create a very simple shared virtual environment using Unity's networking support.

Preparation

  • Nothing

Documentation

Procedure

  1. Empty Scene Make an empty scene with a ground and a directional light. Position the camera such that you have a good view of the ground plane.
  2. Create Network Manager Create a new GameObject (GameObject→CreateEmpty) and name it “NetworkManager”. Create a new C# script called “Networking.cs” and attach it to this new game object. Paste the following code into this script:
    using UnityEngine;
    using System.Collections;
     
    public class Networking : MonoBehaviour {
     
    	public GameObject playerPrefab;
    	public Transform spawnObject;
    	bool initialized = false;
     
    	// Use this for initialization
    	void Start () {
     
    	}
     
    	// Update is called once per frame
    	void Update () {
     
    	}
     
    	void OnGUI() {
    		if(!initialized) {
    			if(GUI.Button (new Rect(10,10,100,30),"Start Server")) {
    				Network.InitializeServer(32,2500,true);
    			}
    			if(GUI.Button (new Rect(10,100,100,30),"Start Client")) {
    				Network.Connect ("127.0.0.1",2500);
    			}
    		}
     
    	}
     
        void OnServerInitialized() {
    		initialized = true;
    		Debug.Log ("Server Initialized");
    		spawnPlayer();
    	}
     
    	void OnConnectedToServer() {
    		initialized = true;
    		spawnPlayer();
    	}
     
    	void spawnPlayer() {
    		Network.Instantiate(playerPrefab, spawnObject.position, Quaternion.identity,0);
    	}
    }
  3. Create Player Create a regular cube (GameObject→CreateOther→Cube). Change its name to “NetworkPlayer”. Add a CharacterMotor component to it (Component→Character→CharacterMotor). Add a NetworkView component to it (Component→Miscellaneous→NetworkView). Create a new C# script called “CharacterMovement.cs” and add the following code to it:
    using UnityEngine;
    using System.Collections;
     
    public class CharacterMovement : MonoBehaviour {
     
     	public float speed = 5.0f;
    	public float gravity = 5.0f;
     
    	private CharacterController cc;
     
    	// Use this for initialization
    	void Start () {
    		cc = GetComponent<CharacterController>();
    	}
     
    	// Update is called once per frame
    	void Update () {
    		if(networkView.isMine) {
    			cc.Move (new Vector3(Input.GetAxis ("Horizontal")*speed*Time.deltaTime, -gravity*Time.deltaTime, Input.GetAxis("Vertical")*speed*Time.deltaTime));
    		}
    	}
    }
  4. Turn Player into Prefab Drag your NetworkedPlayer object into the project panel to create a Prefab and delete the original instance in the Hierarchy view.
  5. Create a Spawn Point Create a new empty game object and call it “SpawnPoint”. Place it well above the plane.
  6. Finalize Networking Choose the NetworkManager and drag the NetworkPlayer PreFab into the Player Prefab property and drag the SpawnObject game object into the Spawn Object property.
  7. Build Stand Alone Go to File→Build Settings… and press Player Settings…. Under Resolution check the box marked Run in Background. Build a stand alone application and run it. Press Start Server in the application. Make sure you can move the box with the arrow keys. Start a second instance of the application but chose Start Client this time. Make sure you can move the cube, and see it move in both applications!
/var/www/cadia.ru.is/wiki/data/pages/public/t-vien-14-1/lab_8_materials.txt · Last modified: 2024/04/29 13:33 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki