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 arjwolf1 · Jan 25, 2017 at 08:31 PM · networkingmultiplayernetworkplayermultiplayer networking

Network Player Direction in Unity

I am currently building my first multiplayer game in unity and having a little bit of an issue changing/transmitting the players direction over the network.

My current player controls class is this: using UnityEngine; using System.Collections;

 public class playerControls : MonoBehaviour {
 
     #region
 
     //Vars
     //Movements Vars
     public float runSpeed;
     Rigidbody mybody;
     Animator myAnimator;
     bool playerDirectionE;
 
     #endregion
 
     // Use this for initialization
     void Start () {
         mybody = GetComponent<Rigidbody>();
         myAnimator = GetComponent<Animator>();
         playerDirectionE = true;
         mybody.transform.eulerAngles = new Vector3(0, 90, 0);
 
 
     }
 
     // Update is called once per frame
     void Update () {
 
     }
 
     void FixedUpdate()
     {
         float move = Input.GetAxis("Horizontal");
         myAnimator.SetFloat("speed", Mathf.Abs(move)); //To intiate the charecter transition(move)
 
         mybody.velocity = new Vector3(move * runSpeed, mybody.velocity.y, 0); //move charecter along the x axis, and keep y on gravity, not touching the z axis
 
         if(move>0 && !playerDirectionE)
         {
             Flip();
         }
         else if(move<0 && playerDirectionE)
         {
             Flip();
         }
     }
 
     void Flip()
     {
         playerDirectionE = !playerDirectionE;
         Vector3 theScale = transform.localScale;
         theScale.z *= -1;
         transform.localScale = theScale;
     }
 }

I am using the following client information to send commands over the network:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 using System.Collections.Generic;
 
 public class PlayerSyncRotation : NetworkBehaviour {
 
     [SyncVar(hook ="OnPlayerRotSynced")]
     private float syncPlayerRotation;
 
     [SerializeField]
     private Transform playerTransform;
     private float lerpRate = 15;
 
     private float lastPlayerRot;
     private float threshold = 1;
 
     private List<float> syncPlayerRotList = new List<float>();
     private float closeEneough = 0.3f;
     [SerializeField]
     private bool userHistoricalInterpolation;
 
     [Client]
     void OnPlayerRotSynced(float latestPlayerRotation)
     {
         syncPlayerRotation = latestPlayerRotation;
         syncPlayerRotList.Add(syncPlayerRotation);
     }
 
     [Command]
     void CmdProvideRotationsToServer(float playerRot)
     {
         syncPlayerRotation = playerRot;
     }
 
     [Client]
     void transmitRotations()
     {
         if (isLocalPlayer)
         {
             if(CheckIfBeyondThreshold(playerTransform.localScale.z, lastPlayerRot)){
                 lastPlayerRot = playerTransform.localScale.z;
                 CmdProvideRotationsToServer(lastPlayerRot);
 
             }
         }
     }
 
     bool CheckIfBeyondThreshold(float rot1, float rot2)
     {
         if (Mathf.Abs(rot1 - rot2) > threshold)
         {
             return true;
         }
         else
         {
             return false;
         }
     }
 
     // Use this for initialization
     void Start () {
 
     }
 
     // Update is called once per frame
     void Update () {
         lerpRotation();
     }
 
     void FixedUpdate()
     {
         transmitRotations();
     }
 
     void lerpRotation()
     {
         if (!isLocalPlayer)
         {
                 HistoricalInterpolation();
         }
     }
 
     void HistoricalInterpolation(){
         if (syncPlayerRotList.Count > 0)
         {
             LerpPlayerRotation(syncPlayerRotList[0]);
 
             if(Mathf.Abs(playerTransform.localEulerAngles.z - syncPlayerRotList[0]) < closeEneough)
             {
                 syncPlayerRotList.RemoveAt(0);
             }
             Debug.Log(syncPlayerRotList.Count.ToString() + "syncPlayerRotList Count");
         }
     }
 
     void LerpPlayerRotation(float rotAngle)
     {
         Vector3 playerNewRot = new Vector3(0, 0, rotAngle);
         playerTransform.rotation = Quaternion.Lerp(playerTransform.rotation, Quaternion.Euler(playerNewRot),lerpRate*Time.deltaTime);
     }
 }

My rotation looks fine on the client, but over the network on the second client, the rotation is broken and looks very wrong.

I have attached a link to a Webm where you can see the short video snippet of my problem HERE.

From the Webm you can see how my character rotation is broken, also notice how in my left client the idle character(the character on the right side) is facing right in the left client and facing left in right client.

Would anyone here have any input as to what I could be doing wrong or how I could fix this issue? any suggestions would be appreciated.

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

134 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

Related Questions

Calling NetworkManager.ServerChangeScene() Destroys Player 0 Answers

Best Practice for an object that is both online and offline using uNET 0 Answers

How can I send update to all clients from one client in Multiplayer game in Unity3d? 0 Answers

How to decide an asynchronous multiplayer solution for a turn based mobile chess game? 0 Answers

UNET NetIDs for Scene Objects Mismatched Between Client and Server 1 Answer


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