Unity2D Player movement

Hi,

In the gif above I am basically moving the player in the game view. Now the player has a rigidbody and gravity has not been disabled. I am trying to understand why is the player free to move wherever he wants since gravity should be pulling him down. (This was an example used in our class to show us animations and I have not made these characters)

I have been trying to understand how and why the player can move freely while having a rigidbody 2D as a component.

Hi Evilc,

Which version of Unity do you use? Please do not use an alpha or beta version of Unity for your actual projects. They are full of bugs. Downgrade to a stable version with an “f” in its version number.

Go to Edit > Project Settings > Physics2D and check if the y-component of the gravity is set to -9.81 or something like that.

And how do you move the player? If you override the transform via code each frame, the values calculated by the physics get overridden, too.

Infact it is an alpha version but the other students were using a stable version of 2018 but the movement stayed the same. Also the gravity is set correctly to -9.81.

Is there something I’m missing on how this type of movement can be achieved? I would like to know how this is being done.

The movement is done by getting the horizontal and veritcal axis and then moving with the MovePosition().

Also, I forgot to add that I tried turning off the player movement script and the character stayed there without being dragged by the gravity. I do not think that it is a bug but rather something that I can not figure out why this type of behavior is happenning.

The problem could be the MovePosition method which tries to move your character to a certain position, which is defined by moveVel * Time.fixedDeltaTime. Even if moveVel was 0f, it is still a value, and rb.position is a Vector3 object.

You could test another method, for example, AddForce.


See also:

But as I mentioned, I turned off the script and therefore the script should have no affect on it. I am not sure why this has happened tbh. There might be something I am missing and I might be confusing you aswell.

Change the Body Type of your Rigidbody2D component to “Dynamic”.


See also:

Surprisingly enough, changing to dynamic didn’t have a single affect on the player. But when I disabled the script (therefore the MovePosition() method that is used by the Kinematic rigidbody) it acted normal and the character was dragged down. Thanks for your help appreciate it alot!

You’re welcome. :slight_smile:

Try to use AddForce with the Dynamic mode. The problem in your code is/was that the player gets/got moved towards a specific position via MovePosition instead of a direction via AddForce.

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

Privacy & Terms