Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

168 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Check when rotation of child object has stopped? 0 Answers

Gameobjects spawn differently on client and host. 0 Answers

Multiplayer - Host and Client problem with input. 1 Answer

How to change Player rotation towards nearest other Player? 0 Answers

How do I rotate a Networked gameobject on spawn? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges