Search Unity

Resolved Scripting Parameter Changes for Animation Controller

Discussion in 'Scripting' 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 Animation, 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 velocity locking 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 X axis works as expected as does the +Z, however the -Z now drops below zero.

    velocityerror3.gif

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

    --EDIT--

    Here is the entire script. I try to isolate the problem and only post relevant script excerpts, but in this case I'm not sure where the problem is.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AnimationStateController2D : MonoBehaviour
    6. {
    7.     Animator animator;
    8.     float velocityZ = 0.0f;
    9.     float velocityX = 0.0f;
    10.     public float accelZ = 3.0f;
    11.     public float decelZ = 5.0f;
    12.     public float accelX = 4.0f;
    13.     public float decelX = 4.0f;
    14.     public float maxWalkVel = 0.5f;
    15.     public float maxRunVel = 2.0f;
    16.  
    17.     // increase performance
    18.     int VelocityZHash;
    19.     int VelocityXHash;
    20.  
    21.     // Start is called before the first frame update
    22.     private void Start()
    23.     {
    24.         // search the gameObject this script is attached to and get its animator component
    25.         animator = GetComponent<Animator>();
    26.  
    27.         //increase perfomrance
    28.         VelocityZHash = Animator.StringToHash("Velocity Z");
    29.         VelocityXHash = Animator.StringToHash("Velocity X");
    30.     }
    31.  
    32.     // handles acceleration and deceleration
    33.     void ChangeVelocity(bool forwardPressed, bool leftPressed, bool rightPressed, bool downPressed, bool runPressed, float curMaxVel)
    34.     {
    35.         //if player presses forward, increase velocity in z direction
    36.         if (forwardPressed && velocityZ < curMaxVel)
    37.         {
    38.             velocityZ += Time.deltaTime * accelZ;
    39.         }
    40.  
    41.         // increase velocity in left direction
    42.         if (leftPressed && velocityX > -curMaxVel)
    43.         {
    44.             velocityX -= Time.deltaTime * accelX;
    45.         }
    46.  
    47.         // increase velocity in right direction
    48.         if (rightPressed && velocityX < curMaxVel)
    49.         {
    50.             velocityX += Time.deltaTime * accelX;
    51.         }
    52.  
    53.         // decrease velocityZ
    54.         if (!forwardPressed && velocityZ > 0.0f)
    55.         {
    56.             velocityZ -= Time.deltaTime * decelZ;
    57.         }
    58.         // increase velocityX if left is not presssed and velocityX is less than zero
    59.         if (!leftPressed && velocityX < 0.0f)
    60.         {
    61.             velocityX += Time.deltaTime * decelX;
    62.         }
    63.  
    64.         // decrease velocityX if right is not pressed and velocityX is greater than zero
    65.         if (!rightPressed && velocityX > 0.0f)
    66.         {
    67.             velocityX -= Time.deltaTime * decelX;
    68.         }
    69.     }
    70.  
    71.     void LockOrResetVelocity(bool forwardPressed, bool leftPressed, bool rightPressed, bool downPressed, bool runPressed, float curMaxVel)
    72.     {
    73.         // reset velocityZ
    74.         if (!forwardPressed && !downPressed && velocityZ < 0.0f)
    75.         {
    76.             velocityZ = 0.0f;
    77.         }
    78.  
    79.         // reset velocityX
    80.         if (!leftPressed && !rightPressed && velocityX != 0.0f && (velocityX > -0.05f && velocityX < 0.05f))
    81.         {
    82.             velocityX = 0.0f;
    83.         }
    84.  
    85.         // lock forward
    86.         if (forwardPressed && runPressed && velocityZ > curMaxVel)
    87.         {
    88.             velocityZ = curMaxVel;
    89.         }
    90.         // decelerate to the maximum walk velocity
    91.         else if (forwardPressed && velocityZ > curMaxVel)
    92.         {
    93.             velocityZ -= Time.deltaTime * decelZ;
    94.             // round to the curMaxVel if within offset
    95.             if (velocityZ > curMaxVel && velocityZ < (curMaxVel + 0.05f))
    96.             {
    97.                 velocityZ = curMaxVel;
    98.             }
    99.         }
    100.         // round to the curMaxVel if within offset
    101.         else if (forwardPressed && velocityZ < curMaxVel && velocityZ > (curMaxVel - 0.05f))
    102.         {
    103.             velocityZ = curMaxVel;
    104.         }
    105.  
    106.         ////lock back
    107.         //if (downPressed && runPressed && velocityZ < -curMaxVel)
    108.         //{
    109.         //    velocityZ = -curMaxVel;
    110.         //}
    111.         //// decelerate to the max walk velocity
    112.         //else if (downPressed && velocityZ < -curMaxVel)
    113.         //{
    114.         //    velocityZ += Time.deltaTime * decelZ;
    115.         //    // round to the -curMaxVel if within offset
    116.         //    if (velocityZ < -curMaxVel && velocityZ > (-curMaxVel - 0.05f))
    117.         //    {
    118.         //        velocityZ = -curMaxVel;
    119.         //    }
    120.         //}
    121.         ////round to the curMaxVel if within offset
    122.         //else if (downPressed && velocityZ > -curMaxVel && velocityZ < (-curMaxVel + 0.05F))
    123.  
    124.             // locking left
    125.             if (leftPressed && runPressed && velocityX < -curMaxVel)
    126.         {
    127.             velocityX = -curMaxVel;
    128.         }
    129.         // decelerate to the maximum walk velocity
    130.         else if (leftPressed && velocityX < -curMaxVel)
    131.         {
    132.             velocityX += Time.deltaTime * decelX;
    133.             // round to the curMaxVel if within offset
    134.             if (velocityX < -curMaxVel && velocityX > (-curMaxVel - 0.05f))
    135.             {
    136.                 velocityX = -curMaxVel;
    137.             }
    138.         }
    139.         // round to the curMaxVel if within offset
    140.         else if (leftPressed && velocityX > -curMaxVel && velocityX < (-curMaxVel + 0.05f))
    141.         {
    142.             velocityX = -curMaxVel;
    143.         }
    144.  
    145.         // locking right
    146.         if (rightPressed && runPressed && velocityX > curMaxVel)
    147.         {
    148.             velocityX = curMaxVel;
    149.         }
    150.         // decelerate to the maximum walk velocity
    151.         else if (rightPressed && velocityX > curMaxVel)
    152.         {
    153.             velocityX += Time.deltaTime * decelX;
    154.             // round to the curMaxVel if within offset
    155.             if (velocityX > curMaxVel && velocityX < (curMaxVel + 0.05f))
    156.             {
    157.                 velocityX = curMaxVel;
    158.             }
    159.         }
    160.         // round to the curMaxVel if within offset
    161.         else if (rightPressed && velocityX < curMaxVel && velocityX > (curMaxVel - 0.05f))
    162.         {
    163.             velocityX = curMaxVel;
    164.         }
    165.     }
    166.  
    167.     // Update is called once per frame
    168.     private void Update()
    169.     {
    170.         // Input will be true if the player is pressing on the passed in key paramter
    171.         // get key input from player
    172.         bool forwardPressed = Input.GetKey(KeyCode.W);
    173.         bool leftPressed = Input.GetKey(KeyCode.A);
    174.         bool rightPressed = Input.GetKey(KeyCode.D);
    175.         bool downPressed = Input.GetKey(KeyCode.S);
    176.         bool runPressed = Input.GetKey(KeyCode.LeftShift);
    177.      
    178.         //set current maximum velocity
    179.         float curMaxVel = runPressed ? maxRunVel : maxWalkVel;
    180.  
    181.         // handles changes in velocity
    182.         ChangeVelocity(forwardPressed, leftPressed, rightPressed, downPressed, runPressed, curMaxVel);
    183.         LockOrResetVelocity(forwardPressed, leftPressed, rightPressed, downPressed, runPressed, curMaxVel);
    184.  
    185.         // sets the parameters to our local variable values
    186.         animator.SetFloat(VelocityZHash, velocityZ);
    187.         animator.SetFloat(VelocityXHash, velocityX);
    188.     }
    189. }
    190.  
     
    Last edited: Apr 12, 2021
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    How are you controlling your player?
     
    BenevolenceGames likes this.
  3. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    Another script entirely. Actually a few scripts.


    Player Movement --
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovementController : MonoBehaviour
    6. {
    7.     // The variables that power the character/game interaction
    8.     public float speed = 5.0f;
    9.     private Rigidbody myRigidbody;
    10.  
    11.     private Vector3 moveInput;
    12.     private Vector3 moveVelocity;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         // Delcares the variable as this GameObject's Rigidbody component
    18.         myRigidbody = GetComponent<Rigidbody>();
    19.         // Declares the camera in scene as the variable
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    26.         moveVelocity = moveInput * speed;
    27.     }
    28.  
    29.     void FixedUpdate()
    30.     {
    31.         myRigidbody.velocity = moveVelocity;
    32.     }
    33. }
    34.  
    Look at Mouse --

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LookAtMousePoint : MonoBehaviour
    6. {
    7.     private Camera mainCamera;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         mainCamera = Camera.main;
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
    18.         Plane ground = new Plane(Vector3.up, new Vector3(-50, 10, -50));
    19.         float rayLength;
    20.  
    21.         if (ground.Raycast(cameraRay, out rayLength))
    22.         {
    23.             Vector3 pointToLook = cameraRay.GetPoint(rayLength);
    24.             Debug.DrawLine(cameraRay.origin, pointToLook, Color.blue);
    25.             transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
    26.         }
    27.     }
    28. }
    29.  
    These two are added separately to the player GameObject. I am trying to implement a more modular scripting structure, so that the scripts themselves are universal. TRYING. lol.

    At any rate, this SHOULD mean that the actual velocity of the player Rigidbody is independent from the Velocity parameter within the animator.
     
  4. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    Still struggling with this issue guys. I'm trying to implement a more universal code as well. So that the velocity controlling movement of the Rigidbody also impacts the parameter for the animation controller. Any suggestions?
     
  5. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    --Update SOLVED--

    I figured it out, I think. It works, anyways. Whether or not it will cause bugs is a different story. I can notice some jittery movement on my animation at certain input and character angle combinations. At any rate --

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerAnimationStateController : MonoBehaviour
    6. {
    7.     public float maxVel;
    8.     const float zeroVel = 0.0f;
    9.     public bool isRunning;
    10.     public float curVelX;
    11.     public float curVelZ;
    12.     public float maxWalkVel = 1.0f;
    13.     public float maxRunVel = 2.0f;
    14.     public float moveInputX;
    15.     public float moveInputY;
    16.     private Animator animator;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         animator = GetComponent<Animator>();
    22.     }
    23.     void FixedUpdate()
    24.     {
    25.         isRunning = Input.GetKey(KeyCode.LeftShift);
    26.         moveInputX = Mathf.RoundToInt(Input.GetAxis("Horizontal"));
    27.         moveInputY = Mathf.RoundToInt(Input.GetAxis("Vertical"));
    28.     }
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         maxVel = isRunning ? maxRunVel : maxWalkVel;
    33.  
    34.  
    35.         //locks the x axis to 1.0, 2.0, and 0
    36.  
    37.         if (moveInputX > 0)
    38.         {
    39.             curVelX = maxVel;
    40.         }
    41.         else if (moveInputX < 0)
    42.         {
    43.             curVelX = -maxVel;
    44.         }
    45.         else
    46.         {
    47.             curVelX = zeroVel;
    48.         }
    49.  
    50.         //locks the y axis to 1.0, 2.0, and 0
    51.      
    52.         if (moveInputY > 0)
    53.         {
    54.             curVelZ = maxVel;
    55.         }
    56.         else if (moveInputY < 0)
    57.         {
    58.             curVelZ = -maxVel;
    59.         }
    60.         else
    61.         {
    62.             curVelZ = zeroVel;
    63.         }
    64.  
    65.         animator.SetFloat("Velocity X", curVelX);
    66.         animator.SetFloat("Velocity Z", curVelZ);
    67.      
    68.     }
    69.  
    70. }
    71.  

    produces these results

    animationparametertesting.gif


    Now, I need to figure out how to base the animations playing on the characters rotation since it is locked to the cursor position. Fun.


    --EDIT--

    I had set the maxWalkVel to 0.5 in the Unity Editor. That's why it's locking out there instead of 1 in the gif above. Thought I'd mention that for those with attention to detail.
     
    Last edited: Apr 14, 2021