Search Unity

Netcode + Physics = low frameRate

Discussion in 'NetCode for ECS' started by hardcodednumber, Aug 20, 2020.

  1. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    I am trying to setup my project to have physics on the server and have the clients interpolate. Before I was having issues with my project I, the frame rate was fine. Now that I have recreated it, I am seeing an average of 20 FPS.

    I saw this post here: https://forum.unity.com/threads/physics-netcode-not-syncing-right-or-something.799026/

    I tried what was suggested, but I am still getting low frames. There is a lot of GC going on, that frankly, I don't understand why.

    This is running on the server side:
    Code (CSharp):
    1.  
    2.         protected override JobHandle OnUpdate(JobHandle inputDependencies)
    3.         {
    4.             var group = World.GetExistingSystem<GhostPredictionSystemGroup>();
    5.             var tick = group.PredictingTick;
    6.             var deltaTime = Time.DeltaTime;
    7.             var inputData = default(PlayerInput);
    8.             var job = Entities.ForEach((DynamicBuffer<PlayerInput> inputBuffer, ref Translation translation, ref Rotation rotation, ref CharacterControllerData characterData,
    9.                 ref PhysicsVelocity velocity, ref PredictedGhostComponent prediction) => {
    10.                     if (!GhostPredictionSystemGroup.ShouldPredict(tick, prediction))
    11.                         return;
    12.                     inputBuffer.GetDataAtTick(tick, out inputData);
    13.                     var jump = inputData.jump == PlayerInput.Jumped;
    14.                     float3 Up = new float3(0f, 1f, 0f);
    15.                     var moveDirection = float3.zero;
    16.                     var haveMovement = inputData.forward != 0 || inputData.back != 0 || inputData.left != 0 || inputData.right != 0;
    17.                     //Movement
    18.                     if (haveMovement) {
    19.                         characterData.IsJumping = jump && characterData.IsSupported;
    20.                         if (inputData.forward != 0) {
    21.                             moveDirection = new float3(0, 0, 1);
    22.                         }
    23.                         if (inputData.back != 0) {
    24.                             moveDirection += new float3(0, 0, -1);
    25.                         }
    26.                         if (inputData.left != 0) {
    27.                             moveDirection += new float3(-1, 0, 0);
    28.                         }
    29.                         if (inputData.right != 0) {
    30.                             moveDirection += new float3(1, 0, 0);
    31.                         }
    32.                         characterData.MovementVector = math.normalize(moveDirection);
    33.                     }
    34.                     //rotation
    35.                     var haveRotation = inputData.shootX != 0 || inputData.shootY != 0;
    36.                     if (haveRotation) {
    37.                         var rotSpeed = characterData.RotationSpeed * deltaTime;
    38.                         var shootRotation = quaternion.LookRotation(new float3(inputData.shootX, 0, inputData.shootY), Up);
    39.                         characterData.Rotation = math.slerp(characterData.Rotation, shootRotation, rotSpeed);
    40.                     }
    41.                     else if (!characterData.gamePadConnected) {
    42.                         characterData.MousePosition.y = translation.Value.y;
    43.                         var direction = math.normalize(characterData.MousePosition - translation.Value);
    44.                         characterData.Rotation = quaternion.LookRotation(direction, Up);
    45.                     }
    46.                     //Tweek Velocity
    47.                     float3 linearVelocity = velocity.Linear;
    48.                     linearVelocity *= characterData.MovementDamping;
    49.                     bool isMoving = characterData.IsJumping || (math.lengthsq(characterData.MovementVector) > (characterData.DeadZone * characterData.DeadZone));
    50.                     if (isMoving) {
    51.                         var y = linearVelocity.y;
    52.                         if (characterData.IsSupported) {
    53.                             linearVelocity = characterData.MovementSpeed * characterData.MovementVector;
    54.                             linearVelocity.y = y;
    55.                             if (characterData.IsJumping) {
    56.                                 linearVelocity.y += characterData.JumpSpeed;
    57.                                 characterData.IsJumping = false;
    58.                             }
    59.                         }
    60.                         else {
    61.                             characterData.InitialUnsupportedVelocity *= characterData.MovementDamping;
    62.                             characterData.InitialUnsupportedVelocity.y = y;
    63.                             linearVelocity = characterData.InitialUnsupportedVelocity + (characterData.MovementSpeedInAir * characterData.MovementVector);
    64.                         }
    65.                     }
    66.                     velocity.Linear = linearVelocity;
    67.                     velocity.Angular = float3.zero;
    68.                     rotation.Value = characterData.Rotation;
    69.                 }).Schedule(inputDependencies);
    70.             return job;
    71.         }
    72.  
     

    Attached Files:

  2. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    The most common case for a profiler looking like that is having `Jobs > Leak Detection` set to `Full Stack Trace`. So I would start by checking that setting.
     
  3. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    Ugh, yep. thanks @timjohansson. If you were here, i'd buy you a beer.