Author Topic: TNet's Network Sync Rigidbody Prototype Script: Making it smoother?  (Read 7975 times)

MCoburn

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 7
  • Posts: 69
    • View Profile
There's a lot of things I can do to improve character sync across the network so that it doesn't look choppy. There's also the option of moving colliders forward when you move, but I'm not sure if that is the ideal thing for a shooter.

What I'm doing is trying to use Lerp with a modded version of the stock TNet Sync Rigidbody prototyping script. I've omitted the complete script and only listed the changes:
  1. [RFC(1)]
  2. public float positionLerpFactor = 0.01f;
  3. public float rotationLerpFactor = 0.25f;
  4.  
  5. void OnSync (Vector3 pos, Vector3 rot, Vector3 vel, Vector3 ang) {
  6.         mTrans.position = Vector3.Lerp(mLastPos, pos, positionLerpFactor); // Lerp from the old pos to the new pos
  7.         mTrans.rotation = Quaternion.Lerp(Quaternion.Euler(mLastRot), Quaternion.Euler(rot), rotationLerpFactor); // Lerp the rotation to the new rotation
  8.         mRb.velocity = vel;
  9.         mRb.angularVelocity = ang;
  10.         UpdateInterval();
  11. }
  12.  

The result is a nicer, smooth movement of the player, but when it stops movement the player jerks a little bit forward then snaps back a bit into the correct position. I think this must be due to the Lerp "snapping". Rotation is still a little choppy but it does smooth it out to some extent since I'm updating the Rigidbody data 10 times a second over the network.

There's also this code block here, but I'm not sure if I'll touch this as this is the actual sync function:
  1. public void Sync () {
  2.                 if (TNManager.isInChannel) {
  3.                         UpdateInterval();
  4.                         mWasSleeping = false;
  5.                         mLastPos = mTrans.position;
  6.                         mLastRot = mTrans.rotation.eulerAngles;
  7.                         tno.Send(1, Target.OthersSaved, mLastPos, mLastRot, mRb.velocity, mRb.angularVelocity);
  8.                 }
  9. }
  10.  

So, anything I'm doing wrong or any suggestions?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNet's Network Sync Rigidbody Prototype Script: Making it smoother?
« Reply #1 on: October 26, 2014, 09:43:59 PM »
Get rid of this. Keep the number of syncs low. In Windward I sync rigidbody once every 3 seconds. What I also do is sync the actual input axes values more frequently than that: every 250 ms, or if they change by more than 5%. This results in smooth movement as each player does their own input-based movement logic that gets auto-corrected every 3 seconds. Since my renderer is separated from the rigidbody (using LagPosition/LagRotation), the correction is also fairly smooth. Yes, this means that the colliders are a bit ahead of the renderer, but it's not noticeable at all in the game.

MCoburn

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 7
  • Posts: 69
    • View Profile
Re: TNet's Network Sync Rigidbody Prototype Script: Making it smoother?
« Reply #2 on: October 27, 2014, 01:12:56 AM »
Get rid of this. Keep the number of syncs low. In Windward I sync rigidbody once every 3 seconds. What I also do is sync the actual input axes values more frequently than that: every 250 ms, or if they change by more than 5%. This results in smooth movement as each player does their own input-based movement logic that gets auto-corrected every 3 seconds. Since my renderer is separated from the rigidbody (using LagPosition/LagRotation), the correction is also fairly smooth. Yes, this means that the colliders are a bit ahead of the renderer, but it's not noticeable at all in the game.

So I should chop the amount of syncs by half or more (from 10 to 5 or 3)?

I don't understand 100% what you mean by seperate the renderer from the rigidbody. Do you mean when moving the rigidbody is offset by a certain amount in front of the player and then it is back into place when the player is stationary? What is this LagPosition/LagRotation that you speak of?  ???

voncarp

  • Jr. Member
  • **
  • Thank You
  • -Given: 13
  • -Receive: 2
  • Posts: 91
    • View Profile
Re: TNet's Network Sync Rigidbody Prototype Script: Making it smoother?
« Reply #3 on: October 27, 2014, 03:02:56 AM »
Separating the renderer from the rigidbody works wonders.  I've had some success with this.

After importing the base model with renderers you often see the animator attached to the root gameobject.  What I initially did was add my controls and rigidbody/character controller to this root object.  But, to have it run much smoother during online play you want to add another parent gameobject to the original imported model.  Add your controls and rigidbody/controller to the parent object above the animator/original imported model.

So, what happens is that you move the rigidbody and the child object which should be your model with renderers follows the rigidbody resulting in smoother movement.  The jittery movement that you initially saw still exists, but its now in the root rigidbody/controller which you shouldn't see.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNet's Network Sync Rigidbody Prototype Script: Making it smoother?
« Reply #4 on: October 29, 2014, 05:51:36 AM »
Prefab (Rigidbody, TNObject)
- Collider 1
- Collider 2
- Model (Animation, LagPosition, LagRotation)
-- Renderer 1
-- Renderer 2

creativitysquare

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 41
    • View Profile
Re: TNet's Network Sync Rigidbody Prototype Script: Making it smoother?
« Reply #5 on: November 03, 2014, 11:50:42 AM »
Hello

Following this as trying to implement the lagging child object,

What do you guys mean with RENDER 1, RENDER 2? Do you mean the mesh of the object? the mesh render?
Furthremore, shall i basically make one prefab insider another?

creativitysquare

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 41
    • View Profile
Re: TNet's Network Sync Rigidbody Prototype Script: Making it smoother?
« Reply #6 on: November 03, 2014, 02:01:08 PM »
 I just tried this.
I guess it can work if you move the rigid body game object and the child object having animation doesn't "do" them with root motion.

In my case the object is a character and i am using root motion with animations so no chance to control the rigid body parent as the child would move  via the animations.

I am correct in saying that this technique cannot work with characters on which root motion is applied to the animation - rather then controlling the rigid body above and performing the animations without root motion?

thanks
eg

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNet's Network Sync Rigidbody Prototype Script: Making it smoother?
« Reply #7 on: November 04, 2014, 10:59:07 AM »
That's correct. Root motions won't properly work with multiplayer games. It's one of those single-player-only features in Unity.

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: TNet's Network Sync Rigidbody Prototype Script: Making it smoother?
« Reply #8 on: January 31, 2015, 02:51:54 AM »
an advanced tutorial covering the discussion in this thread would be very helpful.

edit:  specifically, sync'ing several players that are using player-controllers.  spawn-sync, jumping, sync animations, rotation, position, etc.
« Last Edit: February 01, 2015, 05:34:48 PM by devomage »

MCoburn

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 7
  • Posts: 69
    • View Profile
Re: TNet's Network Sync Rigidbody Prototype Script: Making it smoother?
« Reply #9 on: February 11, 2015, 10:58:49 PM »
What I learnt is that Rigidbodies are hardware dependent, and can be a pain in the backside to sync. You also run the risk of having sudden "spaz attacks" if players are lagging and then suddenly they go inside of a rigidbody you're controlling. For my shooter project's controller, I'm simply using a character controller to move the character, and hit detection is using rigid bodies on another layer that move with the female mesh animations.

You can use Rigidbodies over the network, but you'll have to do some safeguards.

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: TNet's Network Sync Rigidbody Prototype Script: Making it smoother?
« Reply #10 on: February 12, 2015, 01:43:04 AM »
i was able to get an "ok" sync'd avatar.  however, it'd be nice to get a decent example using multiplayer CharacterController rather than rigidbody.