I’m trying to establish a client-client connection in unity for a game, and i’m making the unit movement sincronization, so far i actualy archieve than both (i.e. the one acting like a host and the one connecting to it, as Host and Client respectibly) to sincronize movement, i can see the movement of the first (host) in both, but the problem is client is not moving, it try to move, i see a little movement but then it return instantly to him’s place, this is just visible in the Client window, as Host doesn’t see this at all.
I mean, when i move with the host, both, Host and Client can see the movement of the host, when i move the Client, the client character make a little movement, and that movement doesn’t change the character position (because return to the original instantly) and it is not seen in the screen of host.
So the question is:
Why just one of the clients is receiving the input instruction from both clients?
Is there a way to test this properly?, and how i can fix it? here’s the code:
CharacterPlus.cs (The one who make them move when pressed a key)
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class CharacterPlus : NetworkBehaviour {
private Rigidbody2D rb2d;
float input_x;
float input_y;
void Start () {
if (isLocalPlayer) {
rb2d = this.GetComponent<Rigidbody2D>();
}
}
// Update is called once per frame
void FixedUpdate () {
input_x = Input.GetAxisRaw ("Horizontal");
input_y = Input.GetAxisRaw ("Vertical");
if (Input.GetKeyDown (KeyCode.D)||Input.GetKeyDown(KeyCode.RightArrow))
{
Move (1f,0.0f);
}
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
{
Move (-1f,0.0f);
}
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
{
Move (0.0f,1f);
}
if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
{
Move (0.0f,-1f);
}
}
void Move(float h,float v){
Vector3 movement = new Vector3 (h,v, 0f);
rb2d.AddForce (movement*70);
//shadow.transform.position(new Vector3(shadow.transform.x,shadow.transform.y,shadow.transform.y));
}
}
PlayerSetup.css (the one who supposedly must prepare it to avoid this problem to happend)
using UnityEngine;
using UnityEngine.Networking;
public class PlayerSetup : NetworkBehaviour {
[SerializeField]
Behaviour[] componentsToDisable;
void Start()
{
if (!isLocalPlayer) {
for (int i = 0; i < componentsToDisable.Length; i++) {
componentsToDisable *.enabled = false;*
-
}*
-
} else {*
-
GameObject.Find ("SceneCamera").GetComponent<Camera> ().enabled = false;*
-
GameObject.Find ("SceneCamera").GetComponent<AudioListener> ().enabled = false;*
-
GameObject.Find ("CameraPlayer").GetComponent<Camera> ().enabled = true;*
-
GameObject.Find ("CameraPlayer").GetComponent<AudioListener> ().enabled = true;*
-
}*
-
}*
-
void OnDisable()*
-
{*
-
//Maybie, if the player dies, well need to place something*
-
}*
}
Any Suggestion, constructive comment or question would be much apreciated too.
Thanks in advance