• 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 /
  • Help Room /
avatar image
0
Question by Fewpwew130 · May 23, 2019 at 08:16 PM · rigidbodynetworkingphotonlagsmooth

[Simple] [Networking] Smoothing a Rigidbody

Hello, fellows. I've been using Photon Unity Networking 2 package for a simple multi-player project. It works, but the stutter and lag smudges the experience a lot.


Could you please help?


I have a Player prefab with a Rigidbody attached (arePhotonView and PhotonRigidbodyView too). This prehab has the script listed bellow. Could you please help me finding out what is wrong? Thank you in advance.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Photon.Pun; //for photon view is mine
 
 public class net_Player : MonoBehaviourPun, IPunObservable {  //for photon view is mine
 
     public float moveHorizontal;
     public float moveVertical;
     public float movement_speed;
     public Vector3 movement ;
 
     public GameObject movement_controller;
 
     public Vector3 networkPosition;
     public Quaternion networkRotation;
 
 
     void Start () {
         movement_speed = 100f;
         movement_controller = this.gameObject;
     }
 
 
     void FixedUpdate (){
         if (photonView.IsMine) {
 
             //movement
             moveHorizontal = Input.GetAxis ("axis_X");
             moveVertical = Input.GetAxis ("axis_Y");
             movement = new Vector3 (moveHorizontal, 0f, -moveVertical);
             movement_controller.gameObject.GetComponent<Rigidbody> ().velocity = movement * movement_speed;
 
         } else if (photonView.IsMine == false) {
             gameObject.GetComponent<Rigidbody>().position = Vector3.MoveTowards(gameObject.GetComponent<Rigidbody>().position, networkPosition, Time.fixedDeltaTime);
             gameObject.GetComponent<Rigidbody>().rotation = Quaternion.RotateTowards(gameObject.GetComponent<Rigidbody>().rotation, networkRotation, Time.fixedDeltaTime * 100.0f);
         }
     }
 
     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
     {
         if (stream.IsWriting)
         {
             stream.SendNext(this.gameObject.GetComponent<Rigidbody>().position);
             stream.SendNext(this.gameObject.GetComponent<Rigidbody>().rotation);
             stream.SendNext(this.gameObject.GetComponent<Rigidbody>().velocity);
         }
         else
         {
             networkPosition = gameObject.GetComponent<Rigidbody>().position = (Vector3) stream.ReceiveNext();
             networkRotation = gameObject.GetComponent<Rigidbody>().rotation = (Quaternion) stream.ReceiveNext();
             gameObject.GetComponent<Rigidbody>().velocity = (Vector3) stream.ReceiveNext();
 
             float lag = Mathf.Abs((float) (PhotonNetwork.Time - info.SentServerTime));
             networkPosition  += (this.gameObject.GetComponent<Rigidbody>().velocity * lag);
         }
     }
 }
 






Comment
Add comment · Show 1
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 unity_AcFBEaCmdV7CAQ · Jun 26, 2020 at 02:34 AM 0
Share
              gameObject.GetComponent<Rigidbody>().position = Vector3.$$anonymous$$oveTowards(gameObject.GetComponent<Rigidbody>().position, networkPosition, Time.fixedDeltaTime);

Try multiplying fixedDeltaTime*10

 gameObject.GetComponent<Rigidbody>().position = Vector3.$$anonymous$$oveTowards(gameObject.GetComponent<Rigidbody>().position, networkPosition, Time.fixedDeltaTime*10);


1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Captain_Pineapple · May 24, 2019 at 10:34 PM

Hey there,

most probably everything is working as it should, even though i think you dont even need the rigidbody view since you manage the position syncing manually.


your issue probably is in Vector3.MoveTowards. This will result in a constant speed. If your update is too late the image of the player will already have arrived and stop at the last known position until the new one is there. This will only work if you have exact timings and one reeeeaaly specific velocity.


what worked for me was using Lerp. If you choose the lerp value conservative small there should be no stuttering. Note though that this will increase the difference between real and notworked position slightly.

If something was unclear let me know.

A way to simply test this would be to add a transform view and trash the manual pos syncing. there you can test some different ways to update positions.

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 Fewpwew130 · May 29, 2019 at 09:40 AM 0
Share

Thank you for a reply! Unfortunatelly, I saw it some days later after I had moved on away from the Networking, therefore I cannot check your suggestion. But I will return to your advice later for sure, thank you!

I upvoted your comment and rewarded your profile (with a little bit of points) for participation.

In the meanwhile, if you could write a code sample - it will help me (and maybe other users aswell). Thank you again.

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

260 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

Related Questions

Network Rigidbodies act weird? 0 Answers

What's wrong with my script ? 0 Answers

Photon RPC Parameter NullReferenceException 1 Answer

How to get OnTriggerEnter working over a network 2 Answers

I get an error message every time i run this script? Any ideas? 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