Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by Bentley · Jan 02, 2015 at 08:52 AM · 2dphotonlagmultiplayer networking

Lag Compensation with Photon Unity Networking

Hello everybody! I'm trying to make a multiplayer 2D shooter game and I've decided on using Photon. The issue I'm having is that remote characters' movements are choppy, but then if I lerp them they become floaty, and "glide" toward the ground (for lack of a better word) after a jump. Does anyone know of a way to mitigate this floatiness or of a better way to go about smoothing network movements?

This is the current script I have attached to the characters:

 using UnityEngine;
 using System.Collections;
 
 public class NetworkPlayer : Photon.MonoBehaviour
 {
     public Vector3 realPosition = Vector3.zero;
     public float speed = .1f;
     
     void Update ()
     {
         if (!photonView.isMine)
         {
             transform.position = Vector3.MoveTowards(transform.position, realPosition, speed);
         }
     }
 
     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
     {
         if (stream.isWriting)
         {
             stream.SendNext((Vector3)transform.position);
         }
         else
         {
             realPosition = (Vector3)stream.ReceiveNext();
         }
     }
 }
 


Thanks! Bentley

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
2
Best Answer

Answer by Bentley · Jan 02, 2015 at 09:29 AM

Of course right after I ask the question I finally find an answer. This is my updated script. It seems smooth enough for my purposes and there are no more adverse lerp side effects. Hopefully someone else can find a use for this in the future.

 using UnityEngine;
 using System.Collections;
 
 public class NetworkPlayer : Photon.MonoBehaviour
 {
     public Vector3 realPosition = Vector3.zero;
     public Vector3 positionAtLastPacket = Vector3.zero;
     public double currentTime = 0.0;
     public double currentPacketTime = 0.0;
     public double lastPacketTime = 0.0;
     public double timeToReachGoal = 0.0;
     
     void Update ()
     {
         if (!photonView.isMine)
         {
             timeToReachGoal = currentPacketTime - lastPacketTime;
             currentTime += Time.deltaTime;
             transform.position = Vector3.Lerp(positionAtLastPacket, realPosition, (float)(currentTime / timeToReachGoal));
         }
     }
 
     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
     {
         if (stream.isWriting)
         {
             stream.SendNext((Vector3)transform.position);
         }
         else
         {
             currentTime = 0.0;
             positionAtLastPacket = transform.position;
             realPosition = (Vector3)stream.ReceiveNext();
             lastPacketTime = currentPacketTime;
             currentPacketTime = info.timestamp;
         }
     }
 }
 
Comment
Add comment · Show 4 · 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 Keseren · Jun 06, 2016 at 05:08 AM 0
Share

Yes this is the solution. But isn't this just "hiding" the lag? You're only showing an movement between the synchronizing to smooth the visuals. But the actual positions are only updating during the synchronizing. Which means if you're going to create a game where you need some quick reactions (Like an fps game or moba), then the game wouldn't be able to "smooth" out quick turnarounds, since the velocity isn't staying the same so that you are able to predict their movements. $$anonymous$$y photon network project has 6 frames lag between the synchronizing. I can make the movements look smooth, but you can't at all create the quick reactions game I wanted to make. Anyone know any solution to fix the actual lag?

avatar image purpl3grape Keseren · Jan 13, 2017 at 04:42 AM 0
Share

Hi $$anonymous$$eseren,

I haven't quite found a solution that is elegant. However, what I did was measure if the SyncTime has surpassed the delay, when it is expecting the next position. Then if that's the case, in update, the player controlling the object sends a position RPC update. It's a bit hacky, and may potentially lead to the message / second issue if lag is serious. I copied the parts of the script that refer to the RPC sync position when 'needed'

 [PunRPC]
 public void DelayedPlayerPacketSyncPosition(){
     realPosition = tr.position;
 }

 void Update {
     if ( photonView.is$$anonymous$$ine ) {
         //Do nothing -- the character motor/input/etc.. is moving us
         if(syncTime > (syncDelay * 1f)){
             GetComponent<PhotonView> ().RPC ("DelayedPlayerPacketSyncPosition", PhotonTargets.AllBuffered);
         }
     } 
 }

Have you come across any other work arounds in the mean time?

avatar image purpl3grape Keseren · Jan 13, 2017 at 05:09 AM 0
Share

Oh one more comment, I haven't done it yet, but was thinking of one extra smoothing step, is for those players who are not controlling the object. When the syncTime has exceeded, for those guys, they lerp the current position, to the RPCPostion sent by the controlling player.

The effect without this lerp is, the lag becomes significantly shorter than what it was before. Now I want to see if in between these lags, I can smooth them out further. Still testing the RPC count on a decent load fps game 2 players, 3 bots on each $$anonymous$$m (controlled by master client) - Was clocking in at around 180 messages / second in that room. So, not yet the 500 soft limit, which is a good sign, to host smaller scale games.

avatar image purpl3grape Keseren · Jan 13, 2017 at 05:37 AM 0
Share

It doesn't have a significant affect on messages per second before and after. ($$anonymous$$ost of the messages I mentioned previously came from the actual game events, shooting, spawning, etc...)

So from massive lags, dee$$anonymous$$g the online play almost unplayable, and frustrating, the movements are now a lot smoother and when it does lag, the choppiness is much less, though noticeable, but perhaps acceptable for online play.

I am also sending onserialize updates at a rate of 20 per second, and a general sendrate of 33 per second.

avatar image
0

Answer by ragnaken · Mar 22, 2017 at 07:18 AM

thanks, when i saw your code i realised that i have a similar script from photon examples, that is almost the same, then i use it and goes perfect. no more lag. Marcopolotutorial -> NetworkCharacter.cs

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

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.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

Photon - Nothing Working!! Please Help (w/ Video) 0 Answers

Problem with profiler spikes caused by Semaphore.WaitForSignal 0 Answers

Switched Gameobject (weapon) not syncing,Switched Gameobject (weapon) not syncing PHOTON 0 Answers

Is possible to spawn 2d image in photon networking ? 1 Answer

Rendering Lag? Wait for the models to render? 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