Search Unity

Question Switching localscale starts unwanted animation state.

Discussion in 'Scripting' started by JeaNiX, Jan 17, 2022.

  1. JeaNiX

    JeaNiX

    Joined:
    Oct 31, 2016
    Posts:
    20
    If my player is sliding down the wall in a 2D platformer, you can see that whenever I stop sliding so i start the fall animation, for a second it starts the run animation. Slide and Run animation states are not connected, and the problem goes away if I disable the SwitchDirections() function. I really haven't found a way to fix this. Was sitting the whole day trying to change my animation states, but it occurs the problem was in that script. Why is it happening?
    Maybe there is a better solution. flipX is not enough to do, because my object is dependent on other child colliders. They should change their position too. I was thinking maybe a solution is to use flipX with something to change each child scale, but that would bring unnecessary operations.

    Code (CSharp):
    1.          
    2.          private void Update()
    3.          {
    4.              SwitchDirection();
    5.              CheckMovement();
    6.          }
    7.          private void FixedUpdate()
    8.          {
    9.    
    10.          }
    11.          private void SwitchDirection()
    12.          {
    13.              if (_moveInput > 0)
    14.              {
    15.                  FlipAnimation(false);
    16.              }
    17.              else if (_moveInput < 0)
    18.              {
    19.                  FlipAnimation(true);
    20.              }
    21.          }
    22.          private void FlipAnimation(bool isFlipped)
    23.          {
    24.              if (isFlipped)
    25.              {
    26.                  gameObject.transform.localScale = new Vector3(-1, 1, 1);
    27.                  _isFacingRight = false;
    28.              }
    29.              else
    30.              {
    31.                  gameObject.transform.localScale = Vector3.one;
    32.                  _isFacingRight = true;
    33.              }
    34.          }
    _moveInput is just Input.GetAxis("Horizontal");
    _isFacingRight is by default true because the object at start is facing right.
    Watch the video for better understanding.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    The answer to these things is almost always to break the character into multiple parts:

    TheBaseCharacterPrefabWithAllScripts
    AllCollidersGoOnThisObject (Or even the object above)
    VisiblePortionOfGeometryThatMayGetFlippedX
    AnimatorAndAllAnimatedGeometryBelowThis
    OtherThingsLikeWeapons
    Other other things I have no idea what


    In the above, the Animator only controls a small subportion of the hierarchy.

    For flip left/right you only change another small subportion.
     
  3. JeaNiX

    JeaNiX

    Joined:
    Oct 31, 2016
    Posts:
    20
    Thanks, so I guess it is possible to change my object to this solution, without breaking everything. Well. Should I then change the sprites with flipX, and the colliders? Or would it be better to change colliders using the record button in the animator, and just having separate animations for each direction?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    I don't know what would happen if you animated a collider. Sounds dangerous, like a great way to cause collisions to fail and your character to fall through stuff. Do you need to change the collider? There might be some physics awareness tickbox in the animator, I forget.
     
  5. JeaNiX

    JeaNiX

    Joined:
    Oct 31, 2016
    Posts:
    20
    Well, I did try now to change the character, but it seems it can not be done when all of the script and components are already dependent on each other. It's a bad thing because that means there is a complete rebuild of the character needed.
    Would be a great thing, some resource material that would actually explain that, because it never occurred in any of the videos or tutorials I was seeing before. The way to build than a character, so you don't get the same problem.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Not really possible. One works with the information one has at the time one does the work.

    If I was in a GameJam I'd drop a sprite in the scene, slap a collider on and some scripts and start going.

    If I needed more later I would almost certainly expect to tear it all down to add what I needed.

    If someday in the future I need a complicated multi-part animated rolling tucking flying jumping nightmare of a player controller to make a multiplayer diablo clone with sidekicks and rideable mounts, it would be foolish for me to waste my GameJam time trying to anticipate what might be needed for such a thing.
     
  7. JeaNiX

    JeaNiX

    Joined:
    Oct 31, 2016
    Posts:
    20
    True. So, I see there is no way to fix it in the current state?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    I have no idea what your "current state" is.

    I have suggested ways you can engineer towards a suitable solution.
     
  9. JeaNiX

    JeaNiX

    Joined:
    Oct 31, 2016
    Posts:
    20
    Alright, I will begin from scratch trying out that idea and separate things. Thank you.