Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions Multiplayer Lab 02/Assets/Scripts/Common.meta

This file was deleted.

129 changes: 114 additions & 15 deletions Multiplayer Lab 02/Assets/Scripts/Multiplayer/Lab02a_PlayerControl.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,114 @@
using UnityEngine;
using System.Collections;

public class Lab02a_PlayerControl : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
}
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class Lab02a_PlayerControl : NetworkBehaviour {

struct PlayerState
{
public float posX, posY, posZ;
public float rotX, rotY, rotZ;
}

[SyncVar]
PlayerState state;

[Server]
void InitState()
{
state = new PlayerState
{
posX = -119f,
posY = 165.08f,
posZ = -924f,
rotX = 0f,
rotY = 0f,
rotZ = 0f
};
}


//Takes the sync state and sets the local transform to it.
void SyncState()
{
transform.position = new Vector3(state.posX, state.posY, state.posZ);
transform.rotation = Quaternion.Euler(state.rotX, state.rotY, state.rotZ);
}

void Start()
{
InitState();
SyncState();
}

PlayerState Move(PlayerState previous, KeyCode newKey)
{
float deltaX = 0, deltaY = 0, deltaZ = 0;
float deltaRotationY = 0;

switch (newKey)
{
case KeyCode.Q:
deltaX = -0.5f;
break;
case KeyCode.S:
deltaZ = -0.5f;
break;
case KeyCode.E:
deltaX = 0.5f;
break;
case KeyCode.W:
deltaZ = 0.5f;
break;
case KeyCode.A:
deltaRotationY = -1f;
break;
case KeyCode.D:
deltaRotationY = 1f;
break;
} return new PlayerState
{
posX = deltaX + previous.posX,
posY = deltaY + previous.posY,
posZ = deltaZ + previous.posZ,
rotX = previous.rotX,
rotY = deltaRotationY + previous.rotY,
rotZ = previous.rotZ
};
}

void Update()
{
/*
-The script that is utilizing isLocalPlayer MUST extend a NetworkBehaviour
-isLocalPlayer will only return true if it is called from a script that
is attached to a game object that has a NetworkIdentity component
and Local Player Authority enabled
-isLocalPlayer will always return false if it is called in the Awake() function*/
if (isLocalPlayer)
{
KeyCode[] possibleKeys = { KeyCode.A, KeyCode.S, KeyCode.D, KeyCode.W, KeyCode.Q, KeyCode.E, KeyCode.Space };
foreach(KeyCode possibleKey in possibleKeys)
{
if (!Input.GetKey(possibleKey))
{
continue;
}

CmdMoveOnServer(possibleKey);
}
}

SyncState();
}

/*
 A command will only run if isLocalPlayer would return true
 Any method that is tied to a Command MUST start with the Cmd prefix
*/
[Command]
void CmdMoveOnServer(KeyCode pressedKey)
{
state = Move(state, pressedKey);
}
}
Expand Down
Loading