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 Mini-Pao · Sep 07, 2016 at 04:47 PM · scripting problemnetworkingspritenetworkscale

How To Sync Scale in UNET

Im New to networking and Unity as well, so if this is a simple mistake pls bear with me. Im working on a 2d multiplayer project. I have this NetworkBehaviour

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 
 public class LocalPlayerSetup : NetworkBehaviour {
 
 
     void FixedUpdate(){
         flipClients(netFacingRight);
     }
     
     
     void flipClients(bool facing){
         if (!isLocalPlayer) {
             netFacingRight = facing;
             if (netFacingRight)
             {
                 Vector3 flipSprite = this.transform.localScale;
                 flipSprite.x = 1;
                 transform.localScale = flipSprite;
             }
             else
             {
                 Vector3 flipSprite = this.transform.localScale;
                 flipSprite.x = -1;
                 transform.localScale = flipSprite;
             }
         }
         
     }
     
     [SyncVar(hook = "CmdFlipSprite")]
     public bool netFacingRight = true;
     
     
     [Command]
     public void CmdFlipSprite(bool facing)
     {
         netFacingRight = facing;
         if (netFacingRight)
         {
             Vector3 flipSprite = this.transform.localScale;
             flipSprite.x = 1;
             this.transform.localScale = flipSprite;
         }
         else
         {
             Vector3 flipSprite = this.transform.localScale;
             flipSprite.x = -1;
             this.transform.localScale = flipSprite;
         }
     }
 
     void Start () {
         if (isLocalPlayer) {
             GetComponent<PlayerManager>().enabled = true;
         }
     }
 }
 

My problem is that, in the Client app, the host's sprite is not flipping. In the Host i can see everything is working well.

Here is the part of PlayerManager where i call CmdFlipSprite() facingright is Initially true.

     void Update (){
         MovePlayer (speedX);
         if (facingright && speedX < 0 || !facingright && speedX > 0) {
             facingright = !facingright;
             flip();
             local.CmdFlipSprite(facingright);
         }
     }

Hope that someone can help me. Thanks! :)

Comment
Add comment · Show 1
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 AurimasBlazulionis · Mar 28, 2017 at 04:58 PM 0
Share

Hello, do you $$anonymous$$d accepting my answer since it is correct. It really makes me stay motivated to answer these questions.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by SadSwede · Dec 03, 2016 at 05:13 PM

I had the same issue with sprite changing awhile ago but i fixed it by removing my syncvars and using these 2 functions in a script instead.

 [Command]
 public void CmdSpriteChange(int sprite)
 {
     RpcSpriteChange(sprite);
 }

 [ClientRpc]
 void RpcSpriteChange(int sprite)
 {
     if (sprite == 0)
     {
         graphic.sprite = sideways;
     }
     else if (sprite == 1)
     {
         graphic.sprite = forward;
     }
 }

so let's say i want to set a new sprite for EVERY client on the server. To do that i call this function locally:

     CmdSpriteChange(); 

Like this:

  void Start()
  {
       if(isLocalPlayer)
       {
       CmdSpriteChange(1);
       graphic.sprite = forward;
       }
  }

I call the "Cmd" function to make it change on everyone elses screens, and to make it change on yours you just say " graphic.sprite = forward;"

I'am not good at explaining so if it's hard to understand just tell me and ill try one more time ^^

Comment
Add comment · Show 2 · 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
avatar image nativejosh · May 03, 2017 at 03:37 AM 1
Share

I LITERALLY just created an account to thank you.. it has been on my head for TWO WHOLE days!! THAN$$anonymous$$ YOU you kind sir!!!

avatar image SadSwede nativejosh · May 03, 2017 at 04:18 AM 0
Share

No problem man :D

avatar image
0

Answer by AurimasBlazulionis · Sep 07, 2016 at 05:07 PM

First of all, this:

[SyncVar(hook = "CmdFlipSprite")]

Remove the hook. Basically now, the local player will tell the server to run that command once the value changes, other players will not sync the value since they are not allowed to call commands on objects without authority, even if they could, the commands run only on the server.

If you want to flip it, on local player call CmdFlipSprite.

Keep in mind, if you change these things often and need fast reaction, on local player you will have to predict the outcome, this is up to you to implement. The good start would be to create a hook, in which the clients check isLocalPlayer and if it is not true, then blindly accept the change and if it is true, first check if your predictions are correct and if they are not, then change the value.

Comment
Add comment · Show 5 · 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
avatar image Mini-Pao · Sep 07, 2016 at 05:54 PM 0
Share

Sorry misclicked, I was just checking this one out. BTW when you say

If you want to flip it, on local player call CmdFlipSprite.

is it the LocalPlayerSetup.cs or on my Player$$anonymous$$anager?

avatar image AurimasBlazulionis Mini-Pao · Sep 07, 2016 at 07:58 PM 0
Share

Oh right, you do that already. So, if you do not care about predictions, just remove the hook line.

avatar image Mini-Pao AurimasBlazulionis · Sep 08, 2016 at 04:33 AM 0
Share

O$$anonymous$$ ill try this once I get home from school. Thanks for the answer! :)

Show more comments
avatar image Mini-Pao · Sep 09, 2016 at 01:02 PM 0
Share

I removed the hook line but its still the same. Still cant see the host's sprite flip on the client. Should I try to sync their local scales ins$$anonymous$$d?

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

7 People are following this question.

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

Related Questions

Client Sprite cant be controlled when spawned by NetworkManager 1 Answer

what are the basic rules off making a lan server 1 Answer

How to check for a host in a certain area in a open world game? 0 Answers

How to Sync a sprite change to other clients? 0 Answers

Command method dont change variables on server 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