stop_on_slope isn't working

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Entity2020 studios

I have this issue where the player will get stuck on a slope and can’t move up the slope. The only way they can go is back down the slope unless they jump. I am using Godot 4.0.3, so I need to know how to fix this issue in that version.
Things to note:

  1. In Godot 4 and onwards, you can’t add arguements to move_and_slide
  2. I’ve tried setting the max slope angle but that isn’t working
  3. I’ve tried removing move_and_slide from my script but, despite the jump script not needing move_and_slide, jumping stops working when I do this

Is there any way to solve this issue, or is everyone struggling to fix this as much as I am?

If you need to see the code I’m using, then please tell me and I’ll provide the exact bit of code you need to see.

:bust_in_silhouette: Reply From: contentstop

To fix the issue of the player getting stuck on a slope and being unable to move up, you can try implementing the following solution in your Godot 4.0.3 project:

Use a KinematicBody or a KinematicBody2D as your player character. This will allow you to manually handle movement and collisions.

Add a separate collision shape to your player character for detecting slopes. This shape should be positioned at the bottom of your player character and cover the area where the player interacts with the ground.

In your player script, use the move_and_slide() or move_and_slide_with_snap() function to handle movement. Since you mentioned that adding arguments to move_and_slide() is not possible in Godot 4, we’ll focus on move_and_slide_with_snap().

Here’s an example code snippet for a 3D player character:

gdscript
Copy code
extends KinematicBody

const GRAVITY = -9.8
const MAX_SLOPE_ANGLE = 45
const FLOOR_NORMAL = Vector3(0, 1, 0)

var velocity = Vector3()

func _physics_process(delta):
var snap = Vector3(0, 0.01, 0) # Small upward snap to avoid getting stuck on slopes
var floor_normal = FLOOR_NORMAL

velocity.y += GRAVITY * delta
velocity = move_and_slide_with_snap(velocity, floor_normal, snap, MAX_SLOPE_ANGLE)

# Add your other movement and input handling code here

And here’s an example code snippet for a 2D player character:

gdscript
Copy code
extends KinematicBody2D

const GRAVITY = 500
const MAX_SLOPE_ANGLE = 45
const FLOOR_NORMAL = Vector2(0, 1)

var velocity = Vector2()

func _physics_process(delta):
var snap = Vector2(0, 0.01) # Small upward snap to avoid getting stuck on slopes
var floor_normal = FLOOR_NORMAL

velocity.y += GRAVITY * delta
velocity = move_and_slide_with_snap(velocity, floor_normal, snap, MAX_SLOPE_ANGLE)

# Add your other movement and input handling code here

Adjust the values of GRAVITY, MAX_SLOPE_ANGLE, and FLOOR_NORMAL according to your game’s requirements.

Ensure that your slope surfaces have appropriate collision shapes and materials assigned to them. The collision shapes should cover the area where the player character interacts with the ground.

By using the move_and_slide_with_snap() function with a small upward snap value, you can help the player character avoid getting stuck on slopes. The MAX_SLOPE_ANGLE parameter allows you to define the maximum angle of slopes the player can traverse.

Content Stop offers student academic assistance through its assignment writing service. Providing students with reliable and high-quality solutions to alleviate their stress is key to helping them manage their academic workload. You can expect the best quality work from them.

I forgot to mention that move_and_slide_with_snap isn’t in godot 4, and that it’s now just a part of move_and_slide. There is, however, a value in the kinematicbody called ‘snap length’ that I think might help with my issue.

Entity2020 studios | 2023-06-21 15:21