Forum rules - please read before posting.

Animator Controller bug + AC

I'm working on a 2d game which mostly uses very thin navmeshes. I thought my walk animations worked fine until today, when I made one where the player is holding a flashlight, and the light is a sprite protruding forwards. This revealed that, when walking along a thin navmesh (specially when it slopes up or down for a bit), the pathfinding will often make the character face the opposite direction for a split of second. This happens so fast it is not visible when I am using a simple character sprite - but because the light from the flashlight is much longer and brighter, it's very easy to see it turning backwards and forwards again.

I thought, "No problem! I just have to edit the controller and add a tiny exit time between my animations, so that by the time they are ready to transition, the character is already facing forward again." So I checked "Has Exit Time" and tried playing with the times. The problem here is that the checkbox is broken. No matter how small I set the Exit Time (I even tried a 0 exit time), if it is checked at all, the previous animation will play in its entirety before the transition happens. I've googled this bug, and the consensus has been "don't use Has Exit Time" for years and years.

So, sadly, I can't fix this via the controller. I was wondering if I could have a workaround via AC - I think the pathfinding works well, and it seems silly to change it only for this reason. All I need is to be able to feed the animator controller a FacingAngle parameter that is smoothed over, e.g. one that doesn't change unless the actual FacingAngle has been different for longer than, say, 0.1 s. How can I do this?

Comments

  • The issue itself may be resolved by tweaking your settings. Have you tried enabling Retro-style movement? in the Player's Inspector? That should prevent any overshooting that may be causing the behaviour.

    Enabling Angle snapping may also alleviate this - it's a bit hard to tell without seeing the issue.

    As for smoothing over FacingAngle - yes, a simple script attached to the Animator can do this. Keep the Player's Inspector as it is, but create a second Float Parameter named "FacingAngle_Smoothed" and use this to transition between your different animations instead.

    using UnityEngine;
    
    public class SmoothAngle : MonoBehaviour
    {
    
        public float speed = 5f;
        private Animator _animator;
    
        void Start ()
        {
            _animator = GetComponent <Animator>();
        }
    
        void Update ()
        {
            float rawAngle = _animator.GetFloat ("FacingAngle");
            float smoothAngle = _animator.GetFloat ("FacingAngle_Smoothed");
            smoothAngle = Mathf.Lerp (smoothAngle, rawAngle, Time.deltaTime * speed);
            _animator.SetFloat ("FacingAngle_Smoothed", smoothAngle);
        }
    
    }
    
  • Retro-style movement was already enabled, and Angle snapping didn't help because the angle would still snap all the way to the opposite direction.

    But the script worked perfectly, thank you! Problem solved.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.