Search Unity

Question 2D Blend Tree Parameters (Movement and velocity locking issues)

Discussion in 'Animation' started by BenevolenceGames, Apr 12, 2021.

  1. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    So I need some help again guys, this time with some animation scripting. I wasn't sure whether this issue should be posted here or in scripting, but here goes.

    I have a script that should lock out the Velocity X and Velocity Z parameters at 2.0 for running and 0.5 for walking. For some reason the +X axis works fine as does the +Z axis, but the -X axis and the -Z axis are both broken. The -Z doesn't register velocity change at all, and the -X doesn't lock properly.
    VelocityError.gif

    Here is the code powering the player in the video above --
    Code (CSharp):
    1.  void LockOrResetVelocity(bool forwardPressed, bool leftPressed, bool rightPressed, bool downPressed, bool runPressed, float curMaxVel)
    2.     {
    3.         // reset velocityZ
    4.         if (!forwardPressed && !downPressed && velocityZ < 0.0f)
    5.         {
    6.             velocityZ = 0.0f;
    7.         }
    8.  
    9.         // reset velocityX
    10.         if (!leftPressed && !rightPressed && velocityX != 0.0f && (velocityX > -0.05f && velocityX < 0.05f))
    11.         {
    12.             velocityX = 0.0f;
    13.         }
    14.  
    15.         // lock forward
    16.         if (forwardPressed && runPressed && velocityZ > curMaxVel)
    17.         {
    18.             velocityZ = curMaxVel;
    19.         }
    20.         // decelerate to the maximum walk velocity
    21.         else if (forwardPressed && velocityZ > curMaxVel)
    22.         {
    23.             velocityZ -= Time.deltaTime * decelZ;
    24.             // round to the curMaxVel if within offset
    25.             if (velocityZ > curMaxVel && velocityZ < (curMaxVel + 0.05f))
    26.             {
    27.                 velocityZ = curMaxVel;
    28.             }
    29.         }
    30.         // round to the curMaxVel if within offset
    31.         else if (forwardPressed && velocityZ < curMaxVel && velocityZ > (curMaxVel - 0.05f))
    32.         {
    33.             velocityZ = curMaxVel;
    34.         }
    35.  
    36.         //lock back
    37.         if (downPressed && runPressed && velocityZ < -curMaxVel)
    38.         {
    39.             velocityZ = -curMaxVel;
    40.         }
    41.         // decelerate to the max walk velocity
    42.         else if (downPressed && velocityZ < -curMaxVel)
    43.         {
    44.             velocityZ += Time.deltaTime * decelZ;
    45.             // round to the -curMaxVel if within offset
    46.             if (velocityZ < -curMaxVel && velocityZ > (-curMaxVel - 0.05f))
    47.             {
    48.                 velocityZ = -curMaxVel;
    49.             }
    50.         }
    51.         //round to the curMaxVel if within offset
    52.         else if (downPressed && velocityZ > -curMaxVel && velocityZ < (-curMaxVel + 0.05F))
    53.  
    54.             // locking left
    55.             if (leftPressed && runPressed && velocityX < -curMaxVel)
    56.         {
    57.             velocityX = -curMaxVel;
    58.         }
    59.         // decelerate to the maximum walk velocity
    60.         else if (leftPressed && velocityX < -curMaxVel)
    61.         {
    62.             velocityX += Time.deltaTime * decelX;
    63.             // round to the curMaxVel if within offset
    64.             if (velocityX < -curMaxVel && velocityX > (-curMaxVel - 0.05f))
    65.             {
    66.                 velocityX = -curMaxVel;
    67.             }
    68.         }
    69.         // round to the curMaxVel if within offset
    70.         else if (leftPressed && velocityX > -curMaxVel && velocityX < (-curMaxVel + 0.05f))
    71.         {
    72.             velocityX = -curMaxVel;
    73.         }
    74.  
    75.         // locking right
    76.         if (rightPressed && runPressed && velocityX > curMaxVel)
    77.         {
    78.             velocityX = curMaxVel;
    79.         }
    80.         // decelerate to the maximum walk velocity
    81.         else if (rightPressed && velocityX > curMaxVel)
    82.         {
    83.             velocityX += Time.deltaTime * decelX;
    84.             // round to the curMaxVel if within offset
    85.             if (velocityX > curMaxVel && velocityX < (curMaxVel + 0.05f))
    86.             {
    87.                 velocityX = curMaxVel;
    88.             }
    89.         }
    90.         // round to the curMaxVel if within offset
    91.         else if (rightPressed && velocityX < curMaxVel && velocityX > (curMaxVel - 0.05f))
    92.         {
    93.             velocityX = curMaxVel;
    94.         }
    95.     }


    Now, if I remove the backwards movement from the script --



    Code (CSharp):
    1.  void LockOrResetVelocity(bool forwardPressed, bool leftPressed, bool rightPressed, bool downPressed, bool runPressed, float curMaxVel)
    2.     {
    3.         // reset velocityZ
    4.         if (!forwardPressed && !downPressed && velocityZ < 0.0f)
    5.         {
    6.             velocityZ = 0.0f;
    7.         }
    8.  
    9.         // reset velocityX
    10.         if (!leftPressed && !rightPressed && velocityX != 0.0f && (velocityX > -0.05f && velocityX < 0.05f))
    11.         {
    12.             velocityX = 0.0f;
    13.         }
    14.  
    15.         // lock forward
    16.         if (forwardPressed && runPressed && velocityZ > curMaxVel)
    17.         {
    18.             velocityZ = curMaxVel;
    19.         }
    20.         // decelerate to the maximum walk velocity
    21.         else if (forwardPressed && velocityZ > curMaxVel)
    22.         {
    23.             velocityZ -= Time.deltaTime * decelZ;
    24.             // round to the curMaxVel if within offset
    25.             if (velocityZ > curMaxVel && velocityZ < (curMaxVel + 0.05f))
    26.             {
    27.                 velocityZ = curMaxVel;
    28.             }
    29.         }
    30.         // round to the curMaxVel if within offset
    31.         else if (forwardPressed && velocityZ < curMaxVel && velocityZ > (curMaxVel - 0.05f))
    32.         {
    33.             velocityZ = curMaxVel;
    34.         }
    35.  
    36.         ////lock back
    37.         //if (downPressed && runPressed && velocityZ < -curMaxVel)
    38.         //{
    39.         //    velocityZ = -curMaxVel;
    40.         //}
    41.         //// decelerate to the max walk velocity
    42.         //else if (downPressed && velocityZ < -curMaxVel)
    43.         //{
    44.         //    velocityZ += Time.deltaTime * decelZ;
    45.         //    // round to the -curMaxVel if within offset
    46.         //    if (velocityZ < -curMaxVel && velocityZ > (-curMaxVel - 0.05f))
    47.         //    {
    48.         //        velocityZ = -curMaxVel;
    49.         //    }
    50.         //}
    51.         ////round to the curMaxVel if within offset
    52.         //else if (downPressed && velocityZ > -curMaxVel && velocityZ < (-curMaxVel + 0.05F))
    53.  
    54.             // locking left
    55.             if (leftPressed && runPressed && velocityX < -curMaxVel)
    56.         {
    57.             velocityX = -curMaxVel;
    58.         }
    59.         // decelerate to the maximum walk velocity
    60.         else if (leftPressed && velocityX < -curMaxVel)
    61.         {
    62.             velocityX += Time.deltaTime * decelX;
    63.             // round to the curMaxVel if within offset
    64.             if (velocityX < -curMaxVel && velocityX > (-curMaxVel - 0.05f))
    65.             {
    66.                 velocityX = -curMaxVel;
    67.             }
    68.         }
    69.         // round to the curMaxVel if within offset
    70.         else if (leftPressed && velocityX > -curMaxVel && velocityX < (-curMaxVel + 0.05f))
    71.         {
    72.             velocityX = -curMaxVel;
    73.         }
    74.  
    75.         // locking right
    76.         if (rightPressed && runPressed && velocityX > curMaxVel)
    77.         {
    78.             velocityX = curMaxVel;
    79.         }
    80.         // decelerate to the maximum walk velocity
    81.         else if (rightPressed && velocityX > curMaxVel)
    82.         {
    83.             velocityX += Time.deltaTime * decelX;
    84.             // round to the curMaxVel if within offset
    85.             if (velocityX > curMaxVel && velocityX < (curMaxVel + 0.05f))
    86.             {
    87.                 velocityX = curMaxVel;
    88.             }
    89.         }
    90.         // round to the curMaxVel if within offset
    91.         else if (rightPressed && velocityX < curMaxVel && velocityX > (curMaxVel - 0.05f))
    92.         {
    93.             velocityX = curMaxVel;
    94.         }
    95.     }
    The other 3 axis work as expected.
    velocityerror3.gif

    I'm unsure where I'm going wrong. Thank you in advance for any help.