Author Topic: Unity Sample Assets beta third person controller + TNet  (Read 5322 times)

ckrin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Unity Sample Assets beta third person controller + TNet
« on: February 25, 2014, 12:53:24 AM »
hey guys,

I'm currently playing around with the new sample assets (beta) from unity and i tried to create a fluid movement with the new third person controller.
i never worked with physics over the network till yet, so i thought it would be a good idea to learn this with the new assets since it uses rigidbody instead of character controller.
i couldn't figure out what i was doing wrong, but things looked like my first steps in networking when i was syncing transform positions  ::)

anyway, i removed the scripts and tried out the basic way to sync the movement vectors. the new scripts are well organised and the input is separated to the logic.
i couldn't believe my eyes when two lines of code almost did the job and now its a bit awkward to ask for help ::)

i basically only changed the move function to an RFC
  1. [RFC]
  2.         public void Move (Vector3 move, bool crouch, bool jump, Vector3 lookPos) {
  3.  
  4.                 if (move.magnitude > 1) move.Normalize();
  5.  
  6.                 // transfer input parameters to member variables.
  7.                 this.moveInput = move;
  8.                 this.crouchInput = crouch;
  9.                 this.jumpInput = jump;
  10.                 this.currentLookPos = lookPos;
  11.  
  12.                 // grab current velocity, we will be changing it.
  13.                 velocity = rigidbody.velocity;
  14.  
  15.                 ConvertMoveInput (); // converts the relative move vector into local turn & fwd values
  16.                
  17.                 TurnTowardsCameraForward (); // makes the character face the way the camera is looking
  18.  
  19.                 PreventStandingInLowHeadroom (); // so the character's head doesn't penetrate a low ceiling
  20.  
  21.             ScaleCapsuleForCrouching (); // so you can fit under low areas when crouching
  22.  
  23.                 ApplyExtraTurnRotation(); // this is in addition to root rotation in the animations
  24.                
  25.                 GroundCheck (); // detect and stick to ground
  26.  
  27.                 SetFriction (); // use low or high friction values depending on the current state
  28.  
  29.                 // control and velocity handling is different when grounded and airborne:
  30.                 if (onGround) {
  31.                         HandleGroundedVelocities();
  32.                 } else {
  33.                         HandleAirborneVelocities();
  34.                 }
  35.        
  36.                 UpdateAnimator (); // send input and other state parameters to the animator
  37.  
  38.                 // reassign velocity, since it will have been modified by the above functions.
  39.                 rigidbody.velocity = velocity; 
  40.  
  41.  
  42.         }

and extended the input script with the tno.send:

  1.                  
  2.                         [...]
  3.                         // pass all parameters to the character control script
  4.                         character.Move( move, crouch, jump, lookPos );
  5.                         tno.Send("Move", Target.OthersSaved, move, crouch, jump, lookPos);
  6.  


all inputs are applied correctly but it seems like there are some velocity issues. the synced clone shifts from walk to run about 2 seconds later than the original player, which leads to a huge position difference. i can't find the mistake  :-\
maybe one of you guys already played around with the new third person controller and have a solution.
i attach both scripts. thanks in advance!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unity Sample Assets beta third person controller + TNet
« Reply #1 on: February 25, 2014, 03:13:43 PM »
You will want to pass the velocity with your RFC call. You basically need to pass all the relevant information. You can't rely on that information to be up to date inside the function.

ckrin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Unity Sample Assets beta third person controller + TNet
« Reply #2 on: February 26, 2014, 09:43:41 PM »
thanks for the super fast support.
i have been testing things out for a while now and the problem still occurs :-\

i pass the rigidbody.velocity with the send function

  1. tno.Send("Move", Target.Others, move, crouch, jump, lookPos, rigidbody.velocity);

and added a little check in the move function which checks if object should use its rigidbody.velocity or if it should used the passed "netVelocity".
  1.                         if(tno.isMine)
  2.                         {
  3.                                 // grab current velocity, we will be changing it.
  4.                                 velocity = rigidbody.velocity;
  5.                         }
  6.                         else
  7.                         {
  8.                                 // grab param velocity, if not my object.
  9.                                 velocity = netVelocity;
  10.                         }
  11.  

move function now looks like followed:

  1. [RFC]
  2.         public void Move (Vector3 move, bool crouch, bool jump, Vector3 lookPos, Vector3 netVelocity) {
  3.  
  4.                         if (move.magnitude > 1) move.Normalize();
  5.  
  6.                         // transfer input parameters to member variables.
  7.                         this.moveInput = move;
  8.                         this.crouchInput = crouch;
  9.                         this.jumpInput = jump;
  10.                         this.currentLookPos = lookPos;
  11.                        
  12.                         if(tno.isMine)
  13.                         {
  14.                                 // grab current velocity, we will be changing it.
  15.                                 velocity = rigidbody.velocity;
  16.                         }
  17.                         else
  18.                         {
  19.                                 // grab param velocity, if not my object.
  20.                                 velocity = netVelocity;
  21.                         }
  22.                         ConvertMoveInput (); // converts the relative move vector into local turn & fwd values
  23.                        
  24.                         TurnTowardsCameraForward (); // makes the character face the way the camera is looking
  25.  
  26.                         PreventStandingInLowHeadroom (); // so the character's head doesn't penetrate a low ceiling
  27.  
  28.                     ScaleCapsuleForCrouching (); // so you can fit under low areas when crouching
  29.  
  30.                         ApplyExtraTurnRotation(); // this is in addition to root rotation in the animations
  31.                        
  32.                         GroundCheck (); // detect and stick to ground
  33.  
  34.                         SetFriction (); // use low or high friction values depending on the current state
  35.  
  36.                         // control and velocity handling is different when grounded and airborne:
  37.                         if (onGround) {
  38.                                 HandleGroundedVelocities();
  39.                         } else {
  40.                                 HandleAirborneVelocities();
  41.                         }
  42.                
  43.                         UpdateAnimator (); // send input and other state parameters to the animator
  44.  
  45.                         // reassign velocity, since it will have been modified by the above functions.
  46.                         rigidbody.velocity = velocity; 
  47.  
  48.         }

i debuged the passed velocitys and the look good. i guess the problems core is somewhere in the functions that modify the velocity  :-\
any ideas? :)
« Last Edit: February 26, 2014, 09:52:08 PM by ckrin »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unity Sample Assets beta third person controller + TNet
« Reply #3 on: February 27, 2014, 07:01:44 PM »
No, you should set the velocity only inside the RFC. You shouldn't keep resetting it in your update function.

The idea is to send the latest instance information to everyone on the network, but have them run their own simulations. Periodic corrections will be sent out, adjusting the simulated values.

ckrin

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: Unity Sample Assets beta third person controller + TNet
« Reply #4 on: February 28, 2014, 05:56:07 AM »
No, you should set the velocity only inside the RFC. You shouldn't keep resetting it in your update function.

in my point of view my code is doing this. ???
if object is not mine dont use its rigidbody velocity, but use its netVelocity.
what update function do you mean?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Unity Sample Assets beta third person controller + TNet
« Reply #5 on: February 28, 2014, 08:02:23 PM »
Velocity of the object changes every FixedUpdate due to variety of factors such as friction, hitting other objects, etc.

You are re-setting it to the last sync'd value. You shouldn't do that. Set it only once inside the sync function and leave it alone.