• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Obsdark · Dec 16, 2016 at 07:12 PM · c#unity 5scripting problemmovementnetworking

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Obsdark · Dec 17, 2016 at 02:04 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

305 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I use UNetWeaver.dll during runtime? 0 Answers

Getting a mix of warnings in multiplayer 0 Answers

Making a bubble level (not a game but work tool) 1 Answer

FPSController NOT WORKING 3 Answers

UNet: NetworkManager is not automatically spawning the player prefab when a client connects (though it does spawn the host-player) 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges