A
A
Alexey Fobius2020-12-02 12:03:18
C++ / C#
Alexey Fobius, 2020-12-02 12:03:18

Synchronize objects on client and server, in Unet?

I made multiplayer using Unet

There is an object on the stage (a mask that can be put on a character), the script itself works correctly, for the first and for the second player, but the synchronization leaves much to be desired.

When I put on a mask on my character on the host (server, server creator, whatever), she normally puts on and takes off too, if you look at this case from the side of the client (connected player), you can clearly see how he takes off and puts it on

all the code script :

public class ForMask : NetworkBehaviour
{

    public Transform Formask;

    public Transform Formask2;
    public Transform Drop;
    public bool OnMe = false;
    public bool Resolution = false;
    public bool OnMe2 = false;
    public bool Resolution2 = false;

    float rotationX = 0;
    public float lookSpeed = 2.0f;
    public float lookXLimit = 45.0f;
    public Transform mask;
    public bool TextOver = false;
    public Text MyTestLabel;

    


    // Start is called before the first frame update

    void Start()
    {
        //Formask = GameObject.FindGameObjectWithTag("Formask").transform;
        //Formask2 = GameObject.FindGameObjectWithTag("Formask2").transform;
       //Drop = GameObject.FindGameObjectWithTag("Drop").transform;
    }

    // Update is called once per frame
  



    void Update()
    {
        TextOverObject();
       //Formask = GameObject.FindGameObjectWithTag("Formask").transform;
       //Formask2 = GameObject.FindGameObjectWithTag("Formask2").transform;
       //Drop = GameObject.FindGameObjectWithTag("Drop").transform;

        if (Input.GetKey(KeyCode.Mouse0) && Resolution) //если нажали на лкм и игрок рядом то надеваем на него
       {
         Formask = GameObject.FindGameObjectWithTag("Formask").transform;

         GetComponent<BoxCollider>().enabled = false;
         GetComponent<Rigidbody>().useGravity = false;
         transform.position = Formask.position;
         OnMe = true;
       }
        if (Input.GetKey(KeyCode.Mouse0) && Resolution2)
        {
            Formask2 = GameObject.FindGameObjectWithTag("Formask2").transform;

            GetComponent<BoxCollider>().enabled = false;
            GetComponent<Rigidbody>().useGravity = false;
            transform.position = Formask2.position;
            OnMe2 = true;
        }

        if (Input.GetKey(KeyCode.Mouse1) && Resolution)
       {
            Drop = GameObject.FindGameObjectWithTag("Drop").transform;

            OnMe = false;
            transform.position = Drop.position;
            GetComponent<Rigidbody>().useGravity = true;
            GetComponent<BoxCollider>().enabled = true;
        }
        if(Input.GetKey(KeyCode.Mouse1) && Resolution2)
        {
            Drop = GameObject.FindGameObjectWithTag("Drop").transform;

            OnMe2 = false;
            transform.position = Drop.position;
            GetComponent<Rigidbody>().useGravity = true;
            GetComponent<BoxCollider>().enabled = true;
        }

        if (OnMe)
      {
       transform.position = Formask.position;

           
            transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
        }
        if (OnMe2)
        {
            transform.position = Formask2.position;
            transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
        }

    }

  


    void OnTriggerEnter(Collider col) //проверяем приблизился ли игрок
    {
      if(col.gameObject.tag == "Player")
      {
        Resolution = true;
      }
        else if (col.gameObject.tag == "Player2")
        {
            Resolution2 = true;
        }
    }

    
    void OnTriggerExit(Collider col)
    {
      if(col.gameObject.tag == "Player")
      {
        Resolution = false;
      }
       else if (col.gameObject.tag == "Player2")
        {
            Resolution2 = false;
        }
    }


But from the client side, when I put it on, this is not visible on the host.

in fact, I put on a mask, it can be seen on the client, but at the host it still continues to lie on the table.

I looked on the Internet about this, but I did not find an exhaustive answer, everywhere I just talk about the synchronization of players. It became interesting to me why when the server does something, the client sees it, but in the reverse order there is simply no synchronization.

(I set everything up, the players themselves move correctly and smoothly, but the interaction is lame and on both legs)

What is the question, how can I, on behalf of the client, let the host know that I changed the position of this "mask" when I put it on, and that I changed it back when I took it off?

(it’s not about the mask, it’s not so important, it doesn’t really plow normally on any objects)

I know that the code is a crutch, but at the moment its performance is important to me, not cleanliness

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question