Search Unity

Question Solutions For Buggy Animator Script?

Discussion in 'Scripting' started by BenevolenceGames, Apr 27, 2021.

  1. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    So I have a blender that is supposed to transition between the different movement sequences in relation to player rotation. The problem is when the mouse is centered, the script gets confused and this happens.
    blendanimationscripthelp001.gif

    As you can see, the blending at the end is decent, but when the mouse is centered, it goes to pot.

    Here is the script specifically handling the transitions.

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

    Screenshot (5).png

    I am trying to figure out where to start on a code that will smooth that jagged blending causing the erratic player movement. Thank you in advance for any help.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    The .x, .y, .z and .w fields of a quaternion are NOT what you think they are. They are NOT Euler angles, they are internal quaternion fields and useless outside of this context.

    It might be possible to use transform.eulerAngles.y in this case, but this will be subject to gimbal lock, as all eulerAngles usages are, which is the primary reason we don't use them.

    You may instead wish to track, adjust, clamp and drive your own notion of rotation.

    Notes on clamping camera rotation and NOT using .eulerAngles because of gimbal lock:

    https://forum.unity.com/threads/implement-a-limit-in-camera-movement.1100038/#post-7082380
    https://forum.unity.com/threads/implement-a-limit-in-camera-movement.1100038/#post-7083097
     
  3. BenevolenceGames

    BenevolenceGames

    Joined:
    Feb 17, 2021
    Posts:
    128
    I believe you said this exact same thing to me previously. Sorry, I am still trying to learn about how to properly adjust angles.

    Though for the animation transition, I think I need to make the area right over the player a kind of "dead-zone", where it doesn't report any rotation. It simply retains the last rotation, then sets again when leaving that "dead-zone". This could also double as a menu selection by clicking on the "dead-zone" area.

    So how would I go about creating this area? A plane? A gameObject as a child of the player itself? What would be the least taxing way?

    Considering the gimbal-lock, since my view point is fixed, and the rotation will only need to be handled on 1 axis, shouldn't that prevent the alignment necessary to create the lock?