Skip to content
Snippets Groups Projects
Commit 2a71d9aa authored by jan's avatar jan
Browse files

Multiplayer works over LAN

parent 891f4ac3
No related branches found
No related tags found
No related merge requests found
Showing
with 225 additions and 0 deletions
fileFormatVersion: 2
guid: ddc27d33a85a92d45b897c06e3c0e381
folderAsset: yes
timeCreated: 1456162766
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour
{
void OnCollisionEnter(Collision collision)
{
GameObject hit = collision.gameObject;
Movement hitPlayer = hit.GetComponent<Movement>();
if (hitPlayer != null)
{
Combat combat = hit.GetComponent<Combat>();
combat.TakeDamage(10);
Destroy(gameObject);
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 7986f889fbaf6724ab90bbaac4aeb3d0
timeCreated: 1456232851
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class Combat : NetworkBehaviour
{
public const int maxHealth = 100;
[SyncVar]
public int health = maxHealth;
public void TakeDamage(int amount)
{
if (!isServer)
return;
health -= amount;
if (health <= 0)
{
health = maxHealth;
RpcRespawn();
}
}
[ClientRpc]
void RpcRespawn()
{
if (isLocalPlayer)
transform.position = Vector3.zero;
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: f8160550fa52b374aa2ad4854b115268
timeCreated: 1456231967
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Types;
using UnityEngine.UI;
public class CustomNetworkManager : NetworkManager
{
private const int port = 7777;
public void StartUpHost()
{
if (!NetworkClient.active && !NetworkServer.active)
{
NetworkManager.singleton.networkPort = port;
NetworkManager.singleton.StartHost();
Debug.Log("Started Host");
}
else
Debug.Log("Failed to host!");
}
public void JoinGame()
{
if (!NetworkClient.active && !NetworkServer.active)
{
NetworkManager.singleton.networkAddress = GameObject.Find("IPAdressTextBox").transform.FindChild("Text").GetComponent<Text>().text;
NetworkManager.singleton.networkPort = port;
NetworkManager.singleton.StartClient();
Debug.Log("Joined: " + NetworkManager.singleton.networkAddress);
}
else
Debug.Log("Failed to join!");
}
public void OnLevelWasLoaded(int level)
{
if (level == 0)
{
GameObject.Find("StartHostButton").GetComponent<Button>().onClick.RemoveAllListeners();
GameObject.Find("StartHostButton").GetComponent<Button>().onClick.AddListener(StartUpHost);
GameObject.Find("JoinGameButton").GetComponent<Button>().onClick.RemoveAllListeners();
GameObject.Find("JoinGameButton").GetComponent<Button>().onClick.AddListener(JoinGame);
}
else
{
GameObject.Find("DisconnectButton").GetComponent<Button>().onClick.RemoveAllListeners();
GameObject.Find("DisconnectButton").GetComponent<Button>().onClick.AddListener(NetworkManager.singleton.StopHost);
}
Debug.Log("Level was loaded");
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 87e80f1453feecc4394d603d9e932c89
timeCreated: 1456251564
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class Movement : NetworkBehaviour
{
public GameObject bulletPrefab;
public override void OnStartLocalPlayer()
{
GetComponent<MeshRenderer>().material.color = Color.red;
GetComponentInChildren<Camera>().enabled = true;
GetComponentInChildren<AudioListener>().enabled = true;
}
void Update()
{
if (!isLocalPlayer)
return;
transform.Translate(Input.GetAxis("Horizontal") * 0.1f, 0, Input.GetAxis("Vertical") * 0.1f);
if (Input.GetKeyDown(KeyCode.Space))
CmdFire();
}
[Command]
void CmdFire()
{
GameObject bullet = (GameObject)Instantiate(bulletPrefab, transform.position - transform.forward, Quaternion.identity);
bullet.GetComponent<Rigidbody>().velocity = -transform.forward * 4;
NetworkServer.Spawn(bullet);
Destroy(bullet, 2);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: b01571fcb630c244cbeca23afc38fd8c
timeCreated: 1456162719
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2008
Project("{D0DFE28D-CE6E-A1E8-CCF5-4FA2C651915B}") = "Lockdown", "Assembly-CSharp.csproj", "{EAA0984B-0E55-F932-5E40-C67F6A87C5F6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EAA0984B-0E55-F932-5E40-C67F6A87C5F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EAA0984B-0E55-F932-5E40-C67F6A87C5F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EAA0984B-0E55-F932-5E40-C67F6A87C5F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EAA0984B-0E55-F932-5E40-C67F6A87C5F6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Assembly-CSharp.csproj
EndGlobalSection
EndGlobal
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment