Answer the question
In order to leave comments, you need to log in
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;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question