- Home /
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:
This is my Player object setup:
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.
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.
I'm really struggling to solve this guys... Any help?...
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?
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.
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.
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.
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?
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).
Figured it out but thanks a lot anyway. Posted my answer as well in case it helps someone else.
Your answer
Follow this Question
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