• 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
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

People who like this

0 Show 0
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

  • Sort: 
avatar image
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

People who like this

0 Show 0 · 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

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
Obsdark

People who like this

1 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 Move() 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. ;)

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Networked player movement very choppy, rotation works fine - UNET 1 Answer

The spaceship acceleration script is good ? And how to use it with engine ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges