Forum rules - please read before posting.

Direct Control + Sprite Unity Complex: Player is moving, but MoveSpeed is 0.0f

edited July 2020 in Technical Q&A

[Unity 2019.4.3f1 - AC 1.71.5]

Hello!

When I'm using Direct Control, I want my Player to stop walking when hitting walls as long as they:

  • are not moving diagonally (like along a diagonal wall);
  • are not "cornered", meaning they aren't between two walls.

To do this I resorted to using Sprites Unity Complex.

Motion Control is set to Automatic, but in the Physics Settings Move with Rigidbody2D? and Freeze Rigidbody when Idle? are both set to true. Along with Slow movement near walls, this helped me prevent the Player from piercing through the BoxColliders and avoid much of the "jittering" when pushing against such colliders.

With Mecanim, I am handling different transitions, e.g. from Idle to Walk and vice versa. Hopefully their names are self explanatory:

From Idle to Walk:

From Walk to Idle:

To handle the IsCornered I'm tracking a CollisionCount variable in Player's OnCollisionEnter \ OnCollisionExit, to check whether the Player are colliding with multiple walls (a bit of a hack, but yeah).

That said, and although I haven't really tested all possible occasions, this mostly works.
Still, if the Player are colliding, and I move very slowly, they will start "sliding" against the collision without leaving the Idle animation state.

In other words, the character would move (the magnitude of AC.KickStarter.playerInput.GetMoveKeys() would be greater than 0) but the MoveSpeed would still be 0.0f and this doesn't trigger the switch in the Animator, where all parameters are set to switch to the Walk state when MoveSpeed is greater than 0.1f.

I suppose this is a side-effect of Slow movement near walls being checked, but I think I really need that feature for this to work properly.

Is there anything I could do or am I maybe overthinking this?

Thanks a million in advance.

Comments

  • edited July 2020

    I suspect it's not absolutely 0.0f, it's just that the Animator Controller only shows values to one decimal place, and that's the nearest.

    You could try lowering the transition threshold to 0.0, but the "Slow near walls" feature could be disabled in favour of a custom script that does the same thing by dynamically altering the character's walkSpeedScale.

    This is handled by the Char script's UpdateWallReductionFactor function, which you could likely move into your own script to either update the character's walk speed, or multiply by the Animator's MoveSpeed parameter to create a new speed parameter that you use instead. Then, you can make changes to the feature as necessary.

  • Thanks a lot, Chris. Moving the UpdateWallReductionFactor function on to another script and using a new speed parameter multiplying the Animator's MoveSpeed seems definitely a nudge in the right direction.

    Still surprised by the amount of tweaking this requires, but that's definitely on me! :)

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.