Created
February 4, 2024 16:47
-
-
Save unitycoder/1148630d0b7d944324eb15ee4e2eee70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // code for https://www.youtube.com/watch?v=Z9Zo8BI77gA (see the video how to use it) | |
| // note this is just a prototype idea, not a recommended solution (it can fail/break easily) | |
| using Cinemachine; | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using Unity.Netcode; | |
| using UnityEngine; | |
| public class Starter : MonoBehaviour | |
| { | |
| public static Starter instance; | |
| public CinemachineVirtualCamera vcam; | |
| private void Awake() | |
| { | |
| instance = this; | |
| } | |
| void Start() | |
| { | |
| Debug.Log("Starter Start"); | |
| #if !UNITY_EDITOR && UNITY_SERVER | |
| StartServer(); | |
| #else | |
| Debug.Log("Failed: UNITY_SERVER"); | |
| #endif | |
| } | |
| public void StartHost() | |
| { | |
| Debug.Log("Starting Host"); | |
| NetworkManager.Singleton.StartHost(); | |
| } | |
| public void StartClient() | |
| { | |
| Debug.Log("Starting Client"); | |
| NetworkManager.Singleton.StartClient(); | |
| } | |
| public void StartServer() | |
| { | |
| Debug.Log("Starting Server"); | |
| NetworkManager.Singleton.StartServer(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // code for https://www.youtube.com/watch?v=Z9Zo8BI77gA (see the video how to use it) | |
| // note this is just a prototype idea, not a recommended solution (it can fail/break easily) | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using Unity.Netcode; | |
| using UnityEngine; | |
| //public class TurnGame : NetworkBehaviour | |
| public class TurnGame : MonoBehaviour | |
| { | |
| public static TurnGame instance; | |
| int currentPlayerIndex = 0; | |
| //int currentPlayerId = 0; | |
| //public List<TurnPlayer> players = new List<TurnPlayer>(); | |
| //void Onco | |
| void Start() | |
| { | |
| instance = this; | |
| } | |
| public bool CanIMove(ulong myId) | |
| { | |
| var players = NetworkManager.Singleton.ConnectedClientsList; | |
| var canMove = myId == players[currentPlayerIndex].ClientId; | |
| if (canMove) | |
| { | |
| Debug.Log("CanMove: myId=" + myId + " index = " + currentPlayerIndex + " players[currentPlayerIndex].ClientId = " + players[currentPlayerIndex].ClientId); | |
| currentPlayerIndex = ++currentPlayerIndex % players.Count; | |
| } | |
| else | |
| { | |
| Debug.Log("Cannot move playerID: " + myId); | |
| } | |
| return canMove; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // code for https://www.youtube.com/watch?v=Z9Zo8BI77gA (see the video how to use it) | |
| // note this is just a prototype idea, not a recommended solution (it can fail/break easily) | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using Unity.Netcode; | |
| using Unity.Netcode.Components; | |
| using UnityEngine; | |
| public class TurnPlayer : NetworkBehaviour | |
| { | |
| Vector3Int pos = Vector3Int.zero; | |
| void Start() | |
| { | |
| } | |
| void Update() | |
| { | |
| if (IsLocalPlayer) | |
| { | |
| if (Input.GetKeyDown(KeyCode.W)) MoveServerRpc(0, 1); | |
| if (Input.GetKeyDown(KeyCode.S)) MoveServerRpc(0, -1); | |
| if (Input.GetKeyDown(KeyCode.A)) MoveServerRpc(-1, 0); | |
| if (Input.GetKeyDown(KeyCode.D)) MoveServerRpc(1, 0); | |
| } | |
| } | |
| [ServerRpc] | |
| void MoveServerRpc(int x, int y) | |
| { | |
| // TODO take on networkspawn | |
| var id = GetComponent<NetworkTransform>().OwnerClientId; | |
| if (TurnGame.instance.CanIMove(id) == false) return; | |
| pos += new Vector3Int(x, 0, y); | |
| // clamp to 0-9 | |
| pos.x = Mathf.Clamp(pos.x, 0, 9); | |
| pos.z = Mathf.Clamp(pos.z, 0, 9); | |
| transform.position = pos; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment