# 2D movement: velocity.y mysteriously changes

Hello everybody! I’m new to Unity.
I have a problem makes me crazy.

I have a really simple 2D project.

• One object Rigidbody2D Dynamic

• One single big rectangular texture with a box collider 2d as a floor

• A second floor made by some tiles on a tilemap. The tilemap has a tilemap collider 2d and a composite collider2d ( with a Rigidbody2D static needed from the composite collider ).

I move the Dynamic Rigidbody2D with this script:

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
Rigidbody2D body;

public float moveSpeed = 5f;

void Start()
{
body = GetComponent<Rigidbody2D>();
}

void Update()
{
float dirX = Input.GetAxis("Horizontal");
Vector2 velocity = new Vector2(Vector2.right.x * moveSpeed * dirX, body.velocity.y);
body.velocity = velocity;
}
}
``````

The Rigidbody2D move on the X axis like I want.

The thing I can’t understand is:
why the Y VELOCITY change when I moving on the tilemap with the composite collider? The Y POSITION doesn’t change.
My script doesn’t do nothing on the Y axis.

Instead, on the big texture with a simple box collider 2d, the Y VELOCITY doesn’t change ( as i think should happen also on the tilemap ).

The only thing I can think about is some reaction between the Dynamic Rigidbody2D of the moving object and the tilemap Static Rigidbody2d…but I can’t explain myself what is happening…

Thanks for helping me.

I can only presume you mean because of gravity. I also presume by “change” you mean small fractional changes.

Gravity will apply a force in one direction and the collider will contact another collider and the solver will produce a force to remove the overlap. This results in a small insignificant undulating motion but visually you won’t or shouldn’t see this. A nice visualisation of this effect (albeit on a page discussing a slightly different topic) is here.

Note: Please use code-tags when posting code, it makes it far easier to understand.

1 Like

Thank you for the answer, the code-tags is really cool.

The gravity was the only idea I had, but still… if POSITION Y doesn’t change I will expect no change on the VELOCITY Y. And the really weird thing is that VELOCITY Y change only while I’m moving on the Static Rigidbody2D.

In anycase I will study the link you provide, thanks again.

What kind of velocity change are we talking here? Some tiny fractional value?

The body-type won’t make any difference so unless you can provide more info on the velocity changes we’re talking it’s hard to help. Whilst it might be static, that might be just how you have it set-up and happens to be static.

1 Like

In anycase I see just now looking at my video that the y velocity is raised to -06 ( didn’t notice before ), so maybe i should just use Mathf.Approximately.

It’s showing things like 1.276e-06 which is 0.000001276 meter/sec or 1.276 micrometer/sec (1276 nanometers!) so completely irrelevant and I’m not sure why it’s an issue to you as you won’t see this. It is caused for the reason in the link I provided.

I could go into lots of detail on how physics engines work but maybe it’s enough to know that this is perfectly normal?

1 Like

Yes…Like I said below the video i didn’t notice before the e-06.
I was trying to use the Y velocity as a parameter to check JumpState.

Now that i understood is just raised to -06 I think i can solve my problem using Mathf.Approximately.

Thank you again

1 Like

same thing happened to me. I really dont understand. Why a tilemap collider 2d with composite collider2d affects my characters y velocity? Everything on a basic collider2d is fine.

My character has a falling animation. When it detects ground and if y velocity is below zero, it must play the fall animation. And all these work amazingly unless I try to use a tilemap.

Looks like you didn’t even read the posts above but continued to necro a super old thread. There is no problem, it’s a lack of understanding.

Please create your own thread and post some useful details. Let’s see if it’s the “same problem”.

1 Like