Question by
mrgohut · Sep 25, 2017 at 03:13 PM ·
rotationnetworking
UNET player and child rotation
So i've been trying for hours to fix this but now i have no idea what's wrong (probably everything :D) but anyway.. Here is a video: https://www.youtube.com/watch?v=X8JrXwgvvFg As you can see my player is rotating and it's synced (as you can see by text above player), but the "hand" isn't and i don't know why. Can you help me?
Client.cs
using System.Collections;
using System.Collections.Generic;
using System.Text;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using UnityStandardAssets.Characters.FirstPerson;
public class Player
{
public string playerName;
public GameObject avatar;
public int connectionID;
}
public class ClientManager : MonoBehaviour {
private const int MAX_PLAYERS = 24;
private int port = 5701;
private int hostId;
private int reliableChannel;
private int unreliableChannel;
private int clientID;
private int connectionId;
private float connectionTime;
private bool isConnected = false;
private bool isStarted = false;
private byte error;
private string playerName;
public GameObject playerPrefab;
public Dictionary<int, Player> players = new Dictionary<int, Player>();
public void Connect()
{
string pName = GameObject.Find("NameInput").GetComponent<InputField>().text;
if (pName == "")
{
Debug.LogError("You must type in your nickname before you connect!");
return;
}
playerName = pName;
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_PLAYERS);
hostId = NetworkTransport.AddHost(topo, 0);
connectionId = NetworkTransport.Connect(hostId, "127.0.0.1", port, 0, out error);
connectionTime = Time.time;
isConnected = true;
}
private void Update()
{
if (!isConnected)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("Receiving: " + msg);
string[] splitdata = msg.Split('|');
switch (splitdata[0])
{
case "ASKNAME":
OnAskName(splitdata);
break;
case "CNN":
Debug.LogError(splitdata[1] + " connected!");
SpawnPlayer(splitdata[1], int.Parse(splitdata[2]));
break;
case "DC":
Debug.LogError(splitdata[1] + " disconnected!");
PlayerDisconnected(int.Parse(splitdata[2]));
break;
case "ASKPOSITION":
OnAskPosition(splitdata);
break;
default:
Debug.LogError("Wrong message: " + msg);
break;
}
break;
}
}
private void OnAskPosition(string[] data)
{
if (!isStarted)
return;
//Update everyone else
for (int i = 1; i < data.Length; i++)
{
string[] d = data[i].Split('%');
//Prevent to server from updating us
if (clientID != int.Parse(d[0]))
{
Vector3 position = Vector3.zero;
position.x = float.Parse(d[1]);
position.y = float.Parse(d[2]);
position.z = float.Parse(d[3]);
players[int.Parse(d[0])].avatar.transform.position = position;
players[int.Parse(d[0])].avatar.transform.rotation = Quaternion.Euler(0, float.Parse(d[4]), 0);
players[int.Parse(d[0])].avatar.transform.GetChild(0).rotation = Quaternion.Euler(float.Parse(d[5]), 0, 0);
Debug.LogWarning("THEIR Player rot: " + float.Parse(d[4]));
Debug.LogWarning("THEIR Camera rot: " + float.Parse(d[5]));
}
}
//Send our own position
Vector3 myPosition = players[clientID].avatar.transform.position;
Debug.LogWarning("MY Player rot Y: " + players[clientID].avatar.transform.rotation.eulerAngles.y.ToString());
Debug.LogWarning("MY Cam rot X: " + players[clientID].avatar.transform.GetChild(0).rotation.eulerAngles.x.ToString());
string m = "MYPOSITION|" + myPosition.x.ToString() + '|' + myPosition.y.ToString() + '|' + myPosition.z.ToString() + '|' + players[clientID].avatar.transform.rotation.eulerAngles.y.ToString() + '|' + players[clientID].avatar.transform.GetChild(0).rotation.eulerAngles.x.ToString();
Send(m, unreliableChannel);
}
Server.cs
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class Players
{
public int connectionId;
public string playerName;
public Vector3 position;
public float rotationPlayerY;
public float rotationCameraX;
}
public class ServerManager : MonoBehaviour
{
private const int MAX_PLAYERS = 24;
private int port = 5701;
private int hostId;
private int reliableChannel;
private int unreliableChannel;
private bool isStarted = false;
private byte error;
private List<Players> players = new List<Players>();
private float lastmovementUpdate;
private float movementUpdateRate = 0.05f;
private void Start()
{
NetworkTransport.Init();
ConnectionConfig cc = new ConnectionConfig();
reliableChannel = cc.AddChannel(QosType.Reliable);
unreliableChannel = cc.AddChannel(QosType.Unreliable);
HostTopology topo = new HostTopology(cc, MAX_PLAYERS);
hostId = NetworkTransport.AddHost(topo, port, null);
isStarted = true;
}
private void Update()
{
if (!isStarted)
return;
int recHostId;
int connectionId;
int channelId;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
NetworkEventType recData = NetworkTransport.Receive(out recHostId, out connectionId, out channelId, recBuffer, bufferSize, out dataSize, out error);
switch (recData)
{
case NetworkEventType.ConnectEvent:
Debug.Log("Player " + connectionId + " connected!");
PlayerConnect(connectionId);
break;
case NetworkEventType.DataEvent:
string msg = Encoding.Unicode.GetString(recBuffer, 0, dataSize);
Debug.Log("Receiving from " + connectionId + ": " + msg);
string[] splitdata = msg.Split('|');
switch (splitdata[0])
{
case "NAMEIS":
OnNameIs(connectionId, splitdata[1]);
break;
case "MYPOSITION":
OnMyPosition(connectionId, float.Parse(splitdata[1]), float.Parse(splitdata[2]), float.Parse(splitdata[3]));
OnMyRotation(connectionId, float.Parse(splitdata[4]), float.Parse(splitdata[5]));
break;
default:
Debug.LogError("Wrong message: " + msg);
break;
}
break;
case NetworkEventType.DisconnectEvent:
Debug.Log("Player " + connectionId + " disconnected!");
OnDisconnection(connectionId);
break;
}
//ask player for their position
if(Time.time - lastmovementUpdate > movementUpdateRate && players.Count > 0)
{
lastmovementUpdate = Time.time;
string m = "ASKPOSITION|";
foreach (Players pl in players)
{
//POSITION UPDATE
m += pl.connectionId.ToString() + '%' + pl.position.x.ToString() + '%' + pl.position.y.ToString() + '%' + pl.position.z.ToString() + '%' + pl.rotationPlayerY.ToString() + '%' + pl.rotationCameraX.ToString() + '|';
}
m = m.Trim('|');
Send(m, unreliableChannel, players);
}
}
private void OnMyPosition(int connID, float x, float y, float z)
{
players.Find(q => q.connectionId == connID).position = new Vector3(x, y, z);
}
private void OnMyRotation(int connID, float playerRot, float cameraRot)
{
players.Find(q => q.connectionId == connID).rotationCameraX = cameraRot;
players.Find(q => q.connectionId == connID).rotationPlayerY = playerRot;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612135613im_/https://answers.unity.com/themes/thub/images/avi.jpg)