1
\$\begingroup\$

I'm trying to implement slopes in my game. When I walk up the slope and suddenly stop, here is what happens:

enter image description here

And when I move down, it goes like this:

enter image description here

I have set density to 0.5, friction to 0 on both platform and a player. I have set friction to 0 because otherwise player gets stuck on a wall if he is jumping against it. What could I do to solve this? By the way, I'm using setLinearVelocity for movement:

body.setLinearVelocity(new Vector2(10, body.getLinearVelocity().y));
\$\endgroup\$
  • \$\begingroup\$ I think I fixed movement down, but that movement up glitch is still there \$\endgroup\$ – Gintas_ Jan 4 '16 at 16:17
  • \$\begingroup\$ okay, I fixed them both! Now the only thing left is to stop character from sliding down when he is idle \$\endgroup\$ – Gintas_ Jan 4 '16 at 20:11
1
\$\begingroup\$

I solved this problem differently, than answers sugested me.

First of all, when player isn't clicking move left/right button and doesn't jump, I made velocity Y = 0.

Next, instead of moving player in straight direction like this:

body.setLinearVelocity(new Vector2(-10, body.getLinearVelocity().y)); // LEFT
body.setLinearVelocity(new Vector2(10, body.getLinearVelocity().y)); // RIGHT

I check for a platform angle and then accordingly rotate that vector like this:

float tetha = platformAngle;
velocity.x = (float) (velocity.x * Math.cos(tetha) - velocity.y * Math.sin(tetha));
velocity.y = (float) (velocity.y + velocity.x * Math.sin(tetha) + velocity.y * Math.cos(tetha));

Also, I use this rotated vector only, when player is walking downhill. I noticed, that if he is walking uphill, then on top of the slope he jumps due to the upwards rotated velocity. So when he is walking uphill, I just use a straight vector and scale it accordingly, so that he won't slow down.

\$\endgroup\$
0
\$\begingroup\$

I'm not very familiar with what causes what specific effects when dealing with physics, but it doesn't appear that you are using a standard character controller, which will handle both of these problems for you most of the time.

The first one is usually a result of the slope absorbing some amount of the character movement, slowing it, even as you continue to provide an input force. Yes, even with friction 0. I am not sure how to solve it, necessarily, but the standard asset character motor should not have this problem.

However, the other problem has to do with gravity. Or rather, the lack thereof. It would appear that your gameobject scale and your gravity scalar don't line up. That is, its like having a 400 foot tall character: gravity will take a while to kick in for the character to fall half of its full height (four seconds!) whereas a correctly sized character would fall half its full height in only a fraction.

You will probably want to read over this article on common mistakes in Unity.

Incorrect Scale

In most games, players would assume that the scale of the world is relatable to Earth's scale. They would expect, for example, an enemy falling from a watch tower to fall at the same rate you would perceive on Earth. If the enemy falls too slowly or too quickly, it can detract from the immersion—particularly if the enemy is human-sized!

\$\endgroup\$
  • \$\begingroup\$ but this is not in unity, it's box2d + libgdx \$\endgroup\$ – Gintas_ Jan 4 '16 at 15:10
  • \$\begingroup\$ Ah, somehow I missed that. <eyes coffee, undrunk> Ah that might explain it. Anyway, the same problem for the second half is what is at work. It should give you something to look into. \$\endgroup\$ – Draco18s Jan 4 '16 at 15:11
0
\$\begingroup\$

TL;DR

Your problems will resolve themselves if you applyForce to the object at every frame, instead of setting the velocity.

What's happening

When you set the velocity directly you are not allowing the physics simulation library (box2d) to enforce the normal laws of physics, and the funky behaviour you are witnessing is the result.

Going Uphill: When your character is walking up the hill, the velocity is being reset at every frame. This is because your setVelocity call causes the y velocity to be carried over from the previous step (There is some initial y velocity imparted when your character first steps onto the slope). So when you stop, the code still takes the y velocity from the previous step (which was greater than 0) so your character moves upward.

Going Downhill: The movement pattern here is stranger, but I am still confident (from plenty of experience) that it's caused by setting the velocity based on the previous time step.

Solution

Please, please use applyForce or applyForceToCenter, and set a non-zero friction value so that your character isn't sliding all over the place. If you try that and you are still having problems with your player getting stuck on a wall, then post another question here. This allows box2d to behave as it should, resolving velocities and positions on its own terms. If you want your character to remain upright remember to set isFixedRotation on the body.

I know from experience (lots of time with box2d) that if you are calling setVelocity on every step you are doing something wrong; there is always a better way.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.