Author Topic: Movement Question  (Read 4113 times)

cardmaster316

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Movement Question
« on: February 11, 2015, 10:00:37 AM »
Ive been messing with tryin to smooth out movement as much as possible and although i have managed to get it not too bad there is still a noticable jitter on the other players side, Having looked at the other posts in the forum regarding this matter i produced the code below, So i just want to check if what i ahve done is correct & if there is anything else i can do or change to help, The character im testing has no rigidbody & the mesh renderer is on a child object of which its parent has the character controller.

  1.     private float HInput
  2.     {
  3.         get
  4.         {
  5.             return Input.GetAxis("Horizontal");
  6.         }
  7.     }
  8.     private float VInput
  9.     {
  10.         get
  11.         {
  12.             return Input.GetAxis("Vertical");
  13.         }
  14.     }

  1.         void Update ()
  2.         {
  3.         if (tno.isMine)
  4.             {
  5.             CharacterController controller = GetComponent<CharacterController>();
  6.  
  7.              if(HInput != 0 || VInput != 0)
  8.             {
  9.                 state = AnimationState.Running;
  10.  
  11.                 Vector3 moveDirection = new Vector3(HInput, 0, VInput);
  12.  
  13.                 if (moveDirection != Vector3.zero)
  14.                     transform.rotation = Quaternion.LookRotation(moveDirection);
  15.  
  16.                 controller.Move(moveDirection * 3 * Time.deltaTime);
  17.  
  18.                 Pos = Transform.position;
  19.                 Rot = Transform.rotation;
  20.             }
  21.             else
  22.                 state = AnimationState.Idle;
  23.             }
  24.         }

  1.     private Vector3 oldpos;
  2.     private Quaternion oldrot;
  3.  
  4.     Quaternion Rot
  5.     {
  6.         set { tno.SendQuickly(5, Target.OthersSaved, value); }
  7.     }
  8.  
  9.     Vector3 Pos
  10.     {
  11.         set { tno.SendQuickly(4, Target.OthersSaved, value); }
  12.     }
  13.  
  14.     [RFC(4)]
  15.     void OnPos(Vector3 pos)
  16.     {
  17.         transform.position = Vector3.Lerp(oldpos, pos, 0.1f);
  18.         oldpos = pos;
  19.     }
  20.  
  21.     [RFC(5)]
  22.     void OnRot(Quaternion rot)
  23.     {
  24.         transform.rotation = Quaternion.Lerp(oldrot, rot, 0.1f);
  25.         oldrot = rot;
  26.     }

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Movement Question
« Reply #1 on: February 11, 2015, 07:11:39 PM »
there really, really needs to be more examples on this subject...  check out the lag scripts:

http://www.tasharen.com/forum/index.php?topic=11572.msg53760#msg53760

cardmaster316

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Movement Question
« Reply #2 on: February 11, 2015, 07:44:15 PM »
Yeah think i saw that post my character model dosent use root motions to move and dosent use a rigidbody either, I have been tryin different ways for 2 days now without success, Im wondering if you have to have rigidbody to achieve smooth movement.

Im also surprised there isnt more code examples in general for this product, Most products of this type if you do a search say in google for the product and your issues you will find loads of code examples detailing different methods which you can then use and tweak to your liking for whatever your trying to do but for this product you pretty much find none apart from a few posts on here discussing bits which is a shame.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Movement Question
« Reply #3 on: February 11, 2015, 10:05:21 PM »
Never send messages from Update like that. If you have 60 FPS you are sending 60 messages per second. If you have 1000 FPS you are sending 1000 messages. This is absurd, and will clog your network, forcing your packets to go out in batches instead.

Sync must be done infrequently -- when values change -- and no more than a few times per second.

As devomage pointed out, and as I've explained many times on this forum, there is no way to eliminate jitter in networking, but there is a way to hide it. The best way of hiding it is to separate renderer from the rigidbody (thus the lag script). I used it in the 2nd example that comes with TNet -- the one with the falling cubes.

Multi-purpose game kit uses TNet to sync unit movement and it the position sync is done every few seconds. In between I sync the movement vectors resulting from input, leading to clients doing their own local playback of what would happen to the unit if certain buttons are in a certain state. This leads to smooth results, with minor corrections occurring infrequently.

I did the same thing in Windward. I sync player ship position once every 4 seconds. I sync actual movement axes ~4 times per second when they're changing, and not at all when they don't.

Many people approach networking with some silly notion that "more = smoother", when in fact nothing can be farther from the truth.

cardmaster316

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Movement Question
« Reply #4 on: February 12, 2015, 06:29:02 AM »
As i stated my character dosent currently have a rigidbody but a character controller, Does this mean you have to have a rigidbody for this to work? If not how would it be done without one?

MCoburn

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 7
  • Posts: 69
    • View Profile
Re: Movement Question
« Reply #5 on: February 12, 2015, 07:29:23 PM »
As i stated my character dosent currently have a rigidbody but a character controller, Does this mean you have to have a rigidbody for this to work? If not how would it be done without one?
If you're using a character controller with a rigidbody sync script, then it will fail, because it cannot reference the rigidbody to sync over the network.
CharacterControllers are easier to sync.

What you might want to do is look at the rigidbody "lag" script that ships with TNet. That should give you an understanding with smooth(er) movement, but ultimately you'll need to decide what you want to do. In my shooter project, we use a character controller for players. We'll work out a smooth movement system once it all works, one idea is to get the player's ping and smoothly lerp the offset the renderer (graphics) part of the gameobject forward/behind the actual position of the character controller, thus if jitter does happen, the lerp will "smooth" it out so to speak.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Movement Question
« Reply #6 on: February 13, 2015, 09:20:24 AM »
No, you don't need a rigidbody. You just need to sync what causes your controller to move (ie: changes in input vectors) and let each client run their own simulation. Periodically send the updated position information from tno.isMine client so that others correct for floating point/lag deviations. My point was, never sync so much data in Update. It's really bad practice.