Skip to content

Instantly share code, notes, and snippets.

View SocketWeaver's full-sized avatar

SocketWeaver SocketWeaver

View GitHub Profile
@SocketWeaver
SocketWeaver / Health.cs
Created June 17, 2019 01:59
Modify the Health.cs and the PlayerHealth.cs script to use the new "HP" SyncProperty instead of the protected currentHP variable.
using SWNetwork;
using UnityEngine;
public class Health : MonoBehaviour
{
public int MaxHp = 100;
const string HP = "HP";
public NetworkID networkID;
public SyncPropertyAgent syncPropertyAgent;
@SocketWeaver
SocketWeaver / PlayerWeapon.cs
Created June 17, 2019 00:47
Update player firing state when left mouse button is released
void Update()
{
if (player.Dead || !networkID.IsMine)
{
return;
}
if (Input.GetKeyUp(KeyCode.Alpha1))
{
syncPropertyAgent.Modify(CURRENT_WEAPON, 0);
@SocketWeaver
SocketWeaver / Gun.cs
Created June 17, 2019 00:45
Extracted and synchronized player's firing state
void Update()
{
timer += Time.deltaTime;
if (timer >= effectDisplayTime)
{
gunEffect.StopEffects();
}
if (networkID.IsMine)
@SocketWeaver
SocketWeaver / Gun.cs
Created June 17, 2019 00:05
Change the Gun.cs script so that the weapon GUIs are not affected by other players weapon selection.
using SWNetwork;
using UnityEngine;
public class Gun : MonoBehaviour
{
public float TimeBetweenBullets = 0.2f;
public float Range = 100f;
public LayerMask ShootableMask;
public int Damage;
public int Ammo;
@SocketWeaver
SocketWeaver / PlayerWeapon.cs
Created June 17, 2019 00:00
Update PlayerWeapon.cs to use SyncProperty to sync player's currentWeapon Index
using System.Collections;
using System.Collections.Generic;
using SWNetwork;
using UnityEngine;
public class PlayerWeapon : MonoBehaviour
{
public int CurrentWeapon = 0;
public List<Gun> Guns;
@SocketWeaver
SocketWeaver / GameSceneManager.cs
Created June 16, 2019 23:26
Add a method in the GameSceneManager.cs script to handle the OnReady event of the SceneSpawner.
public void OnSpawnerReady(bool alreadySetup)
{
Debug.Log("OnSpawnerReady " + alreadySetup);
if (!alreadySetup)
{
int spawnPointIndex = Random.Range(0, 3);
NetworkClient.Instance.LastSpawner.SpawnForPlayer(0, spawnPointIndex);
NetworkClient.Instance.LastSpawner.PlayerFinishedSceneSetup();
}
@SocketWeaver
SocketWeaver / PlayerMovement.cs
Created June 16, 2019 23:09
Return early if don't have ownership of the GameObject
void Update()
{
if (player.Dead || !networkID.IsMine)
{
return;
}
.
.
.
}
@SocketWeaver
SocketWeaver / PlayerMovement.cs
Created June 16, 2019 23:05
Camera follows the local player
NetworkID networkID;
void Start()
{
characterController = GetComponent<CharacterController>();
animator = GetComponentInChildren<Animator>();
player = GetComponent<Player>();
networkID = GetComponent<NetworkID>();
@SocketWeaver
SocketWeaver / GameSceneManager.cs
Last active June 13, 2019 20:09
Set the WinnerId SyncProperty to the player who completed the laps first
void OnTriggerEnter(Collider other)
{
GameObject go = other.gameObject;
NetworkID networkID = go.GetComponent<NetworkID>();
// check if the player is the local player
if (networkID.IsMine)
{
lap_ = lap_ + 1;
guiManager.SetLapRecord(lap_, lapsToWin);
@SocketWeaver
SocketWeaver / GameSceneManager.cs
Last active June 13, 2019 20:10
Handle PlayersPressedEnter's event
RemotePropertyAgent remotePropertyAgent;
const string PLAYER_PRESSED_ENTER = "PlayersPressedEnter";
private void Start()
{
guiManager.SetLapRecord(lap_, lapsToWin);
// get a reference of the gameDataManager component
remotePropertyAgent = GetComponent<RemotePropertyAgent>();
}