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 /
avatar image
0
Question by BloodMarked · Jan 31, 2018 at 09:29 AM · networkingsynchronizationnetworkplayersynchronize

[Network Howto] synchronizing custom values with local authority

i want to synchronize custom values, like p.e. the X-position of the player via network, similar to how the networkTransform Component does it.

my code looks something like this:

 [SyncVar]
 float XPos = 0f;

 void Update(){
  if(!isLocalPlayer){
   transform.position.x = XPos; //(simplified, of course you cannot write to transform.position.x)
   return;
  }
  //my Input handling and character controller logic
  XPos = currentXPosition;
 }

now i spawn one of these for each player with local authority.

problems:

-the server-side client is synched perfectly, but somehow the connected client flickers on the server side between its actual position and XPos = 0 while moving, and defaults to Xpos = 0 when standing still. (or always is on 0)

is a syncVar the wrong way to do this? the example https://unity3d.com/de/learn/tutorials/topics/multiplayer-networking/networking-player-health?playlist=29690 seems to make all SynchVar changes on the Server. but i want to change them with local authority, just like the networkTranform

(-where / how do i set the send rate?)

-or is there an example for a custom NetworkTransform Script somewhere? so i can just grab what i need

Comment
Add comment · Show 2
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
avatar image BloodMarked · Jan 31, 2018 at 03:07 PM 0
Share

i found the send rate & channel configuration: https://docs.unity3d.com/ScriptReference/Networking.NetworkSettingsAttribute.html

avatar image BloodMarked · Jan 31, 2018 at 03:47 PM 0
Share

a command seems to be the way to update the server data, yet the documentation reccomends to not do it every frame. i am confused. what is the best practice to do this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BloodMarked · Jan 31, 2018 at 05:16 PM

this is what i came up with to synchronize custom variables.

potential improvements:

-only send when things change unpredictably (position / animator state change etc)

-idk any other recommendations for optimizations etc hugely appreciated

 using System.Collections;
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class simpleNetworkTester : NetworkBehaviour {
 
     Animator anim;
 
     [SyncVar (hook = "OnServerVarUpdate")] 
     float XPos = 0f;
     [SyncVar]
     int animStateHash;
     [SyncVar]
     float animStateTime;
 
     void Start(){
         anim = GetComponent<Animator> ();
     }
 
     void Update () {
         if (!isLocalPlayer)
             return;    //only do input and logic on the own player
 
         Vector3 newPos = transform.position;
         newPos.x += Input.GetAxis ("Horizontal")*.1f;
         transform.position = newPos;
 
         AnimatorStateInfo animSI = anim.GetCurrentAnimatorStateInfo (0);
 
         CmdUpdateServerVars (newPos.x, animSI.fullPathHash, animSI.normalizedTime);
     }
 
     void OnServerVarUpdate(float newValue){
         if (isLocalPlayer)
             return;    //do not apply changes on own player
         
         Vector3 syncPos = transform.position;
         syncPos.x = newValue;
         transform.position = syncPos;    
  
         anim.Play (animStateHash, 0, animStateTime);
     }
 
     [Command(channel = 1)] //send it through the default unreliable channel
     void CmdUpdateServerVars(float newXPos, int _animStateHash, float _animStateTime){
         XPos = newXPos;
         animStateHash = _animStateHash;
         animStateTime = _animStateTime;
     }
 }
 






Comment
Add comment · Share
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

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

107 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

Related Questions

Network Transform Component forcing Object to a specific position + rotation 0 Answers

RPC or Synchronization ? 1 Answer

Network rotation is laggy and slow 2 Answers

ScriptableObjects and SyncVars, is it posible? 1 Answer

SyncVar works with disabled components? 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