Strange Y velocity on flat ground?

I’m facing a bit of a weird issue where moving horizontally creates a strange Y velocity on my character, but only on a tilemap collider. I have two videos to show the effect. Note the Y velocity number on the far right of the gifs:

On a standard Box Collider:

On my tilemap floor with a composite collider:

using a standard, flat box sprite, everything behaves as expected. On the tilemap, which I set up exactly as shown in the course, my character gains positive or negative Y velocity depending on which direction I move. What’s more, the direction that produces a positive or negative Y value in this case seems completely random, and depends on which direction I go first when playtesting. The value also seems to change arbitrarily after jumping or dropping off a ledge and continuing to run.
Nothing in my code for running around produces or alters the Y value in any case, it’s set up exactly as Rick did.

    void Run()
        Vector2 playerVelocity = new Vector2(moveInput.x * playerSpeed, rigid2d.velocity.y);
        rigid2d.velocity = playerVelocity;
        animator.SetBool("isRunning", Mathf.Abs(rigid2d.velocity.x) > Mathf.Epsilon);

While this doesn’t seem to have much of an effect on gameplay at the moment, it does mess with my falling animation, which triggers when velocity.y < Mathf.Epsilon. Anyone have an idea what causes this, or how to fix it?

Hi Fenmox,

I can’t help but the movement of your character in the second video looks absolutely adorable. :slight_smile:

First of all, check the collider shape of the tilemap collider. If you see any gaps, that could explain the strange behaviour of your character. In that case, disable the tilemap collider component and enable it again to make Unity regenerate the shape.

If that wasn’t the problem, try to set the Collision Detection of the character’s Rigidbody2D component to “Continuous”.

Did this fix it?

See also:

Hi Nina!

Glad you’re liking the lil catsnake! I’m hoping I can give him even more moves later on.

As for the problem, my character’s Rigidbody has already been set to Continuous before taking these videos. There also aren’t any gaps in the collider - the platforms use a tilemap collider that is fed into a composite collider. The outline looks solid and I can’t spot any errors in how it’s been combined.

I managed to find a rough workaround for the animation issue, at least, by setting the threshold for playing the “fall” animation to -0.1f, instead of 0 or Epsilon. Still, it’d be nice to know why my platforms seem to produce upward and downward momentum out of nowhere, and how to potentially stop that from happening.

Using a higher threshold is a good idea. The physics simulation works with floats, and floats are imprecise. The physics simulation is optimised for speed at the expense of precision. When walking, you could get a y-velocity close to 0 but not exactly 0 because of this imprecision.

The alternative would be to rewrite the code for the movement, or maybe write your own physics simulation.

In my personal opinion, a higher threshold is the best solution for your problem.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms