Unity3D конвертирует простую рекурсивную игру змея в многопользовательскую игру змея
М новый в UNET. Я хочу превратить простую рекурсивную игру змея в многопользовательскую игру змея. Это мой код SnakeHead:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class SnakeHead : NetworkBehaviour {
[SyncVar]
public string playerName = "Player";
[SyncVar]
public Color playerColor = Color.white;
private int maxSize;
[SyncVar]
private int currentSize = 0;
private int NESW;
private Vector2 nextPos;
private Vector2 currentPos;
private float currentDeltaTimer;
private float minDeltaTimer = 0.05f;
private float maxDeltaTimer = 0.2f;
private float stepDeltaTimer = 0.2f;
public GameObject snakeTailPrefab;
private SnakeTail snakeTail;
private Vector2 touchOrigin = -Vector2.one;
public int xBound = 40;
public int yBound = 40;
public override void OnStartLocalPlayer () {
currentDeltaTimer = maxDeltaTimer;
InvokeRepeating ("TimerInvoke", 0, currentDeltaTimer);
}
void Start () {
GameObject snakeHolder = new GameObject ("Snake_"+playerName);
snakeHolder.transform.position = Vector3.zero;
transform.parent = snakeHolder.transform;
GetComponent<MeshRenderer>().material.color = playerColor;
}
void Update () {
if (!isLocalPlayer) {
return;
}
if (Input.touchCount > 0)
{
Touch myTouch = Input.touches[0];
if (myTouch.phase == TouchPhase.Began)
{
touchOrigin = myTouch.position;
}
else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0)
{
Vector2 touchEnd = myTouch.position;
float x = touchEnd.x - touchOrigin.x;
float y = touchEnd.y - touchOrigin.y;
touchOrigin.x = -1;
if (Mathf.Abs (x) > Mathf.Abs (y)) {
if (x > 0) {
MobileChangeDirection(1);
} else {
MobileChangeDirection(3);
}
} else {
if (y > 0) {
MobileChangeDirection(0);
} else {
MobileChangeDirection(2);
}
}
}
}
}
private void Movement(){
currentPos = transform.position;
switch (NESW) {
case 0:
nextPos = new Vector2 (currentPos.x, currentPos.y + 1);
break;
case 1:
nextPos = new Vector2 (currentPos.x + 1, currentPos.y);
break;
case 2:
nextPos = new Vector2 (currentPos.x, currentPos.y - 1);
break;
case 3:
nextPos = new Vector2 (currentPos.x - 1, currentPos.y);
break;
}
transform.position = nextPos;
if (!snakeTail) {
currentSize++;
GameObject tempTail = (GameObject)Instantiate (snakeTailPrefab, nextPos, transform.rotation);
snakeTail = tempTail.GetComponent<SnakeTail>();
tempTail.name = "SnakeTrail (" + currentSize + ")";
snakeTail.FixParent (transform.parent);
tempTail.GetComponent<MeshRenderer>().material.color = playerColor;
NetworkServer.Spawn (tempTail);
}
snakeTail.Movement (currentPos);
}
void TimerInvoke(){
Movement ();
StartCoroutine (CheckVisible());
}
public void MobileChangeDirection(int swipeDirection){
if (NESW != 2 && swipeDirection == 0) {
NESW = swipeDirection;
}else if (NESW != 3 && swipeDirection == 1) {
NESW = swipeDirection;
}else if (NESW != 0 && swipeDirection == 2) {
NESW = swipeDirection;
}else if (NESW != 1 && swipeDirection == 3) {
NESW = swipeDirection;
}
}
void OnTriggerEnter (Collider other){
if (other.CompareTag ("Food")) {
EatFood ();
Destroy (other.gameObject);
}else if (other.CompareTag("SnakeTail")) {
}
}
public void EatFood(){
if (snakeTail) {
currentSize++;
snakeTail.EatFood (currentSize, playerColor);
}
}
void Wrap(){
if(NESW == 0){
transform.position = new Vector2 (transform.position.x, -(transform.position.y - 1));
}else if(NESW == 1){
transform.position = new Vector2 (-(transform.position.x - 1), transform.position.y);
}else if(NESW == 2){
transform.position = new Vector2 (transform.position.x, -(transform.position.y + 1));
}else if(NESW == 3){
transform.position = new Vector2 (-(transform.position.x + 1), transform.position.y);
}
}
IEnumerator CheckVisible(){
yield return new WaitForEndOfFrame ();
if(!IsInGameArea()){
Wrap ();
}
}
private bool IsInGameArea(){
Vector3 pos = transform.position;
if(pos.x >= -xBound && pos.x <= xBound && pos.y >= -yBound && pos.y <= yBound){
return true;
}
return false;
}
}
и это мой код SnakeTail:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class SnakeTail : NetworkBehaviour {
private Vector2 currentPos;
public GameObject snakeTailPrefab;
private SnakeTail snakeTail;
public void Movement(Vector2 nextPos){
currentPos = transform.position;
if(snakeTail){
snakeTail.Movement (currentPos);
}
transform.position = nextPos;
}
public void FixParent(Transform snakeTransform){
transform.parent = snakeTransform;
}
public void EatFood(int currentSize, Color playerColor){
if (!snakeTail) {
GameObject tempTail = (GameObject)Instantiate (snakeTailPrefab, transform.position, transform.rotation);
tempTail.name = "SnakeTrail (" + currentSize + ")";
snakeTail = tempTail.GetComponent<SnakeTail> ();
snakeTail.FixParent (transform.parent);
tempTail.GetComponent<MeshRenderer>().material.color = playerColor;
NetworkServer.Spawn (tempTail);
} else {
snakeTail.EatFood (currentSize, playerColor);
}
}
}
Я создаю SnakeHead и SnakeTail GameObject Prefab с NetworkIdentity (локальный игрок проверен) и NetworkTransform. SnakeHead GameObject - это мой "Префаб игрока", а SnakeTail - "Зарегистрированный порожденный префаб" в моем NetworkManager. Когда два устройства соединяются друг с другом и играют в игру, SnakeTail не добавляет в Player на хост-устройстве и цвет SnakeTail не меняется на устройстве RemoteClient. Как я могу это исправить?