Move and slide - slope troubles
Asked Answered
M

6

0

I'm using a CharacterBody2D in Godot 4 and my movement is mostly working the way I want, but I'm still having difficulty with ascending slopes.

At first I thought it was just something related to my tile setup, but I still have issues even when creating a StaticBody2D with a rectangle collision shape.

Usually the player cannot ascend slopes at all in the game. They just get stuck running in place. When I introduced the rectangle shape to test, sometimes the player can ascend it, and then the next time I try it won't work, with the game still running, so no code change. You can see this in the video. Descending slopes seems to always work fine. When ascending slopes works at all, the speed still seems to be less than the constant speed that it should be based on the FloorConstantSpeed = true setting. The floor angle is also reported as being lower than the max angle I have configured even when I get stuck.

Here are all the relevant parts of the code I can think of.

Setup code that happens when the player is loaded:

        body2D = GetParent<CharacterBody2D>();
        body2D.FloorStopOnSlope = true;
        body2D.FloorConstantSpeed = true;
        body2D.FloorMaxAngle = (float)Mathf.DegToRad(75d);
        body2D.FloorSnapLength = 3f;
        body2D.FloorBlockOnWall = true;
        body2D.MotionMode = CharacterBody2D.MotionModeEnum.Grounded;

Code to set Velocity's X component based on input

        if (inputX == 0f)
        {
            inputX = -1f * Input.GetActionStrength(DefaultAction.Left);
        }

        // The player wants to move left or right. Let's see if they can do it.
        if (Mathf.Abs(inputX) < LOW_INPUT_X_THRESHOLD)
        {
            // if we're moving very slowly, stop faster
            Velocity.X = (float)Mathf.MoveToward(Velocity.X, 0, STOP_FORCE * GetPhysicsProcessDeltaTime());
        }
        else
        {
            Facing = inputX > 0f ? Directions.Right : Directions.Left;
            Velocity.X = SPEED * movementScale * inputX;
            return true;
        }

Code in _PhysicsProcess() that calls MoveAndSlide

        Velocity.X = Mathf.Clamp(Velocity.X, -MAX_X_SPEED, MAX_X_SPEED);
        if (!body2D.IsOnFloor())
        {
            Velocity.Y = (float)Mathf.Min(CoreConstants.TERMINAL_VELOCITY, Velocity.Y+
                (CoreConstants.DEFAULT_GRAVITY * GravityScale * delta * 2f));
        }

        // now apply the movement
        body2D.Velocity = Velocity;
        body2D.MoveAndSlide();
        // pixel snap
        body2D.GlobalPosition = body2D.GlobalPosition.Round();

I'm hoping this is just some setting or calculation I have wrong here. If I can use the debugger to confirm any theories out there let me know. Hope someone has a suggestion, thank you

Also may I please be verified so that my posts don't need mod approval?

Mudlark answered 12/10, 2023 at 22:36 Comment(0)
L
1

Mudlark It's more likely something in character body's properties than its code but hard to tell without seeing it.

Levona answered 11/11, 2023 at 23:15 Comment(0)
M
0

I'm setting the velocity to a positive X value when this is happening, and after MoveAndSlide is called, the CharacterBody2D's velocity becomes zero

And the floor's angle is below the max angle ... so I can't see why the movement stops.

This is what the collision shapes look like on the player up close

Mudlark answered 14/10, 2023 at 19:10 Comment(0)
M
0

I don't have anything new to add to the description of the problem, but still experiencing this and unsure what could cause the player to be unable to ascend slopes like this. Would appreciate any suggestions. I've updated to 4.2 beta 5 since creating the post

Mudlark answered 11/11, 2023 at 21:1 Comment(0)
L
1

Mudlark Can you reproduce it in a minimal project? Just one character body and a slope.

Levona answered 11/11, 2023 at 21:14 Comment(0)
M
0

I did create a project with the pieces of movement code shown in the OP, but it doesn't seem to display the same issue. So I guess the issue must be somewhere else in my setup or some condition I haven't replicated by making the example project, but I haven't been able to figure out where. So far just continuing to try to see why this might be happening. I don't see anywhere where I could be zeroing out the velocity while going up slopes. If I continue to not be able to figure it out in my project, I'll try copying more of my character related code into the MRP until I can make it happen there.

Mudlark answered 11/11, 2023 at 23:3 Comment(0)
L
1

Mudlark It's more likely something in character body's properties than its code but hard to tell without seeing it.

Levona answered 11/11, 2023 at 23:15 Comment(0)
M
0

Levona

Ah, I was about to screenshot the inspector properties of the CharacterBody2D and then I saw this setting again:

It was formerly on a smaller value like 0.08 and once I read the description, I tried increasing it to 0.5. It seems like this fixed all of my issues! I guess this means there were some sub-pixel collision problems before. Thanks for the prod in the right direction

Mudlark answered 11/11, 2023 at 23:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.