Hi. I'm new here, and I'm making a platformer game. I'm trying to make it so my character sticks to sloped tiles instead of bouncing down them, and the speed at which it travels up and down them is fixed, rather than appears to be affected by gravity.

I've looked at move_and_slide_with_snap and tried to reverse engineer the 2D Platformer Demo (KinematicBody) but I can't get my head round it. Can anybody help?

Here's a video of what I'm experiencing:

https://youtube.com/watch?v=zi_cJEP5l-Y

This is my basic code (I'm currently working on a state machine, coyote time and wall climbing stuff):

extends KinematicBody2D

export (int) var speed = 400
export (int) var jump_speed = -700
export (int) var gravity = 3000
export (float, 0, 1.0) var friction = 0.25
export (float, 0, 1.0) var acceleration = 0.25

var velocity = Vector2.ZERO

func _physics_process(delta):

	var direction = 0

	if Input.is_action_pressed("right"):
		direction += 1
	if Input.is_action_pressed("left"):
		direction -= 1

	if direction != 0:
		velocity.x = lerp(velocity.x, direction * speed, acceleration)
	else:
		velocity.x = lerp(velocity.x, 0, friction)

	$Sprite.flip_h = velocity.x < 0

	velocity.y += gravity * delta

	velocity = move_and_slide(velocity, Vector2.UP,true, 4)

	if Input.is_action_just_pressed("jump"):
		if is_on_floor():
			velocity.y = jump_speed

Thanks for looking :)

Some background to this, if you are at all interested...

I said that I'm new here, but in fact I'm pretty new to game development too. I messed about with RPG Maker years ago, and last year during lockdown I got GameMaker Studio 2 and set about building my 'dream' platform game. Unfortunately, I bounced hard off that software, due to its horrendous UI, unnecessarily convoluted approach and restrictive licensing. I discovered Godot, but put on hold any game dev ambitions in the Summer as it got in the way of my work as a web developer (or vice versa!).

A year on and I'm giving it another shot and am underway with my platformer again, and so far am pretty pleased with the results - and with Godot, which is so much more elegant than GMS2.

Here's a video of what I previously did in GMS2:

https://youtube.com/watch?v=DNqybLQ5LNU

i would start by creating a collision in the walls. when body colliding, turn off or adjust gravity

Try setting velocity.y to zero when is_on_floor is true. That worked for me.

Thanks for your responses.

I've looked into move_and_slide_with_snap and discovered that I could get it working by setting an appropriate 'snap vector', so now my character sticks to slopes. It disables jumping but I can figure out my state machine to sort that out.

However, I've also discovered that the lerp commands I used to smooth out movements is what is causing my character to move up slopes slowly and slide down again if idle, so I'm just trying to work that out.

This is what the physics process of my code looks like now, with the movement smoothing and jump removed until I've worked them out:

	func _physics_process(delta):
	
	var dir = 0
	
	if Input.is_action_pressed("right"):
		dir += 1
	if Input.is_action_pressed("left"):
		dir -= 1
	
	velocity.x = dir * speed
	$Sprite.flip_h = velocity.x < 0
	velocity.y += gravity * delta

	velocity = move_and_slide_with_snap(velocity, Vector2(0,20), Vector2.UP, true, 4)	

21 days later

Here's a simple fix that I just discovered, for preventing the unwanted slope sliding whilst the player was idle, when using the lerp to add acceleration and deceleration.

I shrank the CollisionShape2D/CapsuleShape2D I was using, and added a downward-facing CollisionShape2D/RayShape2D - see the screenshot of my character below (placeholder artwork!)

This was so easy to do and needed no extra code!

I bloody love Godot (when I finally work things out)! I'm really steaming ahead with my platformer / spelunker / metroidvania... I've sussed out cutout animations for all my player states, a dynamic updating world map, progress saving, NPC dialogue, quests, upgrades, inventory, locked doors and keys.

Now all I've got to do to get my character movements 'just right' is work out how to prevent the slow-down and speed-up when going up and down the 45 degree slopes - I might just scrap them and stick to shallower inclines.

In case you are wondering, the extra RayCasts are for detecting if touching a wall so the character can start climbing, with the upper ones for detecting if near a ledge whilst climbing up.

a year later