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 Vandie_Studios · Aug 26, 2015 at 10:14 AM · c#networkingaxis2d-gameplay

How to get Unet to recognise rotation?

When using Unet's default Network manager, Rotation of my sprites isn't synced.

I'm using the following manager setup: alt text

This is my Player object setup: alt text

How I'm rotating the sprite:

 void RLflip(int rflipcode){
         if (rflipcode != flipcode) {
             if(flipcode == 0){
                 transform.Rotate(new Vector3(0,180,0));
                 flipcode = 1;
             }else{
                 transform.Rotate(new Vector3(0,-180,0));
                 flipcode = 0;
             }
         }
     }

Any Ideas?

thanks in advance. Signed, Vandie.

Comment
Add comment · Show 16
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 Vandie_Studios · Aug 26, 2015 at 01:16 PM 0
Share

tried changing the rotation axis as well as trying to manually send rotation data. Unfortunately, I am yet to develop good enough networking knowledge to do so in a way that would work.

avatar image Vandie_Studios · Aug 27, 2015 at 06:11 PM 0
Share

I'm really struggling to solve this guys... Any help?...

avatar image KaldrisRelm · Aug 27, 2015 at 06:37 PM 0
Share

Is the sprite you are trying to rotate the Player Prefab (Set in the 'Spawn Info' dropdown of the Network$$anonymous$$anager Component) or is it a different object?

avatar image Vandie_Studios · Aug 27, 2015 at 08:36 PM 0
Share

Yes. Its the PlayerPrefab.

avatar image KaldrisRelm · Aug 27, 2015 at 09:57 PM 0
Share

I've found that Network Transform can be really finicky for random reasons, trying to replicate your situation caused the Host's object to rotate for the client, but not the clients own one.

Haven't had much experience with 2d work so I wasn't able to figure out a solution for you using transform.Rotate & the Network Transform, but I can give you a solution to your problem using a SyncVar & a Hook ins$$anonymous$$d:

 [SyncVar(hook = "UpdateRot")] public Vector3 SpriteRot;
 void UpdateRot(Vector3 NewPos)
 {
      transform.Rotate (NewPos);
 }

This above code will make each client manually set the rotation, all you need to do is set a Vector3 to the 'SpriteRot' variable as the server. Let me know if it works or not and we can try to get this up and running for you.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Vandie_Studios · Aug 31, 2015 at 06:41 PM

Credit to @KaldrisRelm as without them I would never have figured this out. The script I ended up using was halfway fixed when they posted there final answer but it was based of their second comment.

At the top of my C# script I made sure that the following two variables where there:

     [SyncVar] private int flipcode;
     [SyncVar] private Quaternion SpriteRot; 

I used the following code to flip all of the players on the server rather than the individual clients:

     [Command]void Cmd_RLflip(int rflipcode){
         if (rflipcode != flipcode) {
             if(flipcode == 0){
                 transform.Rotate(new Vector3(0,180,0));
                 flipcode = 1;
             }else{
                 transform.Rotate(new Vector3(0,-180,0));
                 flipcode = 0;
             }
             SpriteRot = transform.localRotation;
         }
     }

And then used one final method to sync it to the clients (which is called each Update) only when there has been a change in direction:

     void SyncRot(){
         if(!isServer && transform.localRotation != SpriteRot){
             transform.localRotation = SpriteRot;
         }
     }

Again this is a heavily fixed version of Kaldris's second comment so I thank them for the help.

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
avatar image
1

Answer by KaldrisRelm · Aug 31, 2015 at 04:51 PM

Looking at the script you linked I see the problems:

  • You're setting the Vector3 SpriteRot to the transform.localRotation every update on the Server.

  • You're also using your RLflip only on the client, which won't transmit the details to the server to distribute.

To correct this you need to do the following:

Setup a Command to send a request to the Server to flip the Sprite when the player hits Left or Right

  • Remove the if(isServer) from your Control function

  • (Optionally) Remove the if(!isServer) from the UpdateRot function


Here's a quick script I built to Sync Rotation based on client input:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;
 
 public class RotationSync : NetworkBehaviour
 {
     [SyncVar(hook = "UpdateRot")] public Vector3 SpriteRot;
 
     void Update ()
     {
         if (isLocalPlayer)
         {
             Control();
         }
     }
 
     void Control()
     {
         if(Input.GetKey(KeyCode.RightArrow))
         {
             CmdFlip(false);
         }
         else if(Input.GetKey(KeyCode.LeftArrow))
         {
             CmdFlip(true);
         }
     }
     
     [Command]
     void CmdFlip(bool Flag)
     {
         if(Flag)
         {
             SpriteRot = new Vector3(-180,0,0);
         }
         else
         {
             SpriteRot = new Vector3(180,0,0);
         }
     }
     
     void UpdateRot(Vector3 NewPos)
     {
         transform.Rotate (NewPos);
     }
 }

As a side note, the above works on its own, you wouldn't want to use the Network Transform component.

Comment
Add comment · Show 3 · 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 Vandie_Studios · Aug 31, 2015 at 05:04 PM 0
Share

I managed to fix the script myself so it looks a little different, however with my own script currently only the host is flipping on both screens... Is that also a problem with this script? If so then how would I go about fixing it?

avatar image KaldrisRelm · Aug 31, 2015 at 06:38 PM 0
Share

Sounds like that's based on the (isServer) section of your Control function. SyncVars can only be set by the Server, so you'll need to use something like the CmdFlip I linked above to transfer the new rotation request from your Client to your Server(Host).

avatar image Vandie_Studios · Aug 31, 2015 at 06:42 PM 0
Share

Figured it out but thanks a lot anyway. Posted my answer as well in case it helps someone else.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# How to have a toggle 1 Answer

Network how to send data/activate function 1 Answer

Multiplayer desynchronize when grabbing an object by code. 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