• 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 dayer · Sep 26, 2013 at 05:51 PM · multiplayerunity 4.2

Simple multiplayer game lag

I made a very simple test game in Unity 4.2. I will briefly explain how it works. It consists of some cubes and a few other objects with native physics. Also there is a Network View attached to a cube prefab (rigidbody is monitored) and it is controlled with mouse movements via AddForce(). Server is hosted using IntializeServer() and every player spawns their own cube prefab that can be moved around. Only data about controlled cube's rigidbody is transferred if I'm correct.

The problem is that the cubes lag and jitter even when both game instances are on the same computer. I am planning to make a multiplayer 2D platformer and I want to achieve a playable amount of lag, like in all multiplayer games, so that everyone can see a rocket flying exactly the same.

I am not asking for a complete solution, just please guide me in the right direction, I'll do all the digging. I've read about RPC, lock-step, deterministic logics, this simple way I used, but I don't understand, what technique I should use in a simple dynamic 2D game similar to the new Unity 4.3 example project. Should I monitor all objects or somehow make them behave exactly the same on all players?

EDIT: I guess I was not clear enough. The controls are fine, I use networkView.isMine and every player only controls one cube. The lag has to do with networking, not wrong controls.

Comment
Add comment · Show 2
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 FLASHDENMARK · Sep 26, 2013 at 05:55 PM 0
Share

How about lerping the player's movements? So that their movements will be smoothed out.

avatar image dayer · Sep 26, 2013 at 08:17 PM 0
Share

Lerping where? You mean I should transfer rigidbody's (or transform's?) information via custom function and lerp it? Or some other way?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by oliver-jones · Sep 26, 2013 at 06:43 PM

It's because every client is controlling everyone else. On your controller script, you need to add:

 if(networkView.isMine){
 
    //Movement controls
 
 }

You will also need to do this for every other inputs you have for your character. Only then will the user be moving his character, and only his character.

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 dayer · Sep 26, 2013 at 07:59 PM 0
Share

I guess I should mention that I took care of that. I can only move my cube fluently, but in the other player's screen it lags and vice versa.

avatar image
0

Answer by Entairex1 · Sep 26, 2013 at 06:30 PM

I had a problem like this a few days ago.

For every player that joined, It'd spawn a sphere that the player could control in 3rd person. But it appeared as though it had lag. By an accident, one of the spheres stopped moving as I kept moving the other player's sphere. I noticed that the control inputs from one player also attempted to move the other spheres.

Say I would push W, my sphere would move forward, and the other spheres would start to jitter as though they attempted to go forward, but were not allowed.

I think I solved this problem by deactivating every script attached to the sphere, and then activated them when for the instantiated object when the player spawned.

Something like this.

 function SpawnPlayer(player : NetworkPlayer){
     var _NewPlayer : GameObject = Network.Instantiate(PlayerPrefab, transform.position, transform.rotation, _PlayerNumber);
     _NewPlayer.GetComponent(moveCharacter).enabled = true;
     _NewPlayer.GetComponent(shootCubes).enabled = true;
     _NewPlayer.GetComponentInChildren.<Camera>().enabled = true;
 }


This was a tricky problem that took me a while to detect and solve. I only just got into networking and I still don't fully understand the concept, but hopefully this help =)

Comment
Add comment · Show 2 · 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 dayer · Sep 26, 2013 at 07:59 PM 0
Share

I guess I should mention that I took care of that. I can only move my cube, but in the other player's screen it lags and vice versa.

avatar image Entairex1 · Sep 26, 2013 at 09:33 PM 0
Share

Have you tried changing the Network View => State Synchronization to Unreliable, and the Rigid body Interpolate to Interpolate/Extrapolate (it has to do with predicting the movement etc. don't remember which is which)

avatar image
0

Answer by Kiloblargh · Sep 26, 2013 at 09:29 PM

Try this:

Don't sync/ monitor anything with the NetworkView.

Don't use rigid body physics for the player characters in a 2d platformer- never mind that it's a networked game. Just don't. Animate them using fake physics. You can use physics in other places in the game, just not for the main characters.

Have each player RPC their position and velocity as two Vector2s; and have a single GameController object with the only NetworkView on it and a script to receive all these RPCs and position all the remote player's characters accordingly.

Don't use Network Instantiate, send the server an RPC that you are whomever and you just made a whatever, and have it run a function to send RPCs to all the other clients telling a function on their GameController script to make a new whatever that belongs to whomever.

It won't be perfect, but I bet this will already look a lot better than what you are doing and be a good place to start.

One other thing: Think about the tree falling in the forest with no one around to hear it. If you are too far from another player to collide with them any time soon, or to shoot them before they move out of range- does it really matter if they really are where they appear to be at that exact moment? Prioritize what you must keep close track of (where there must be lag, make it count) and fake the rest client-side to make it look as lag-free as possible.

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 dayer · Sep 27, 2013 at 07:48 AM 0
Share

I was hoping to make use of the new awesome 4.3 2D tools and Box2D physics. So for example to "multiplayerize" the Bridge Defense demo project I'd need to rewrite everything to use fake physics? Originally it uses rigidbody2d and other new stuff. Sorry I can't upvote your answer, not enough reputation.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Linux Headless -- Application.Quit() doesn't work? 1 Answer

Unity networking tutorial? 6 Answers

Unity Multiplayer Limit 1 Answer

Multiplayer. Destroying player object on Host/Server 2 Answers

Networking RPC calls never sent to other clients 1 Answer


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