- Home /
How to make sure than one script affect the player of the specific client, and not the other
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 [i].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
Answer by Obsdark · Dec 17, 2016 at 02:01 AM
Fix it already, was a problem with the Network Identity Component of the Player Prefab, i forgot to enable the Local Responsability option.
Thanks in any case, for the try.
Cheers up
Answer by Naphier · Dec 17, 2016 at 12:47 AM
CharacterPlus.FixedUpdate is running for all instances in the scene. Add
if (!isLocalPlayer)
return;
To the beginning of FixedUpdate() to block the rest of the method from running.
Not really, if you see you'll notice than the $$anonymous$$ove() Function applies the force to the rigidbody, so, in this part:
void Start () {
if (isLocalPlayer) {
rb2d = this.GetComponent<Rigidbody2D>();
}
}
what you suggest is prevented
Thanks in any case for the try, i give you one point for that. ;)
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
How do I use UNetWeaver.dll during runtime? 0 Answers
Getting a mix of warnings in multiplayer 0 Answers
Refresh panel with prefab contained value from json that created using array 0 Answers
Coroutine fires once after destroying gameobjects in a scene and recreating new objects 0 Answers