Skip to content

Instantly share code, notes, and snippets.

@XANOZOID
Last active February 7, 2022 02:59
Show Gist options
  • Save XANOZOID/93e8fe66c437da5da0354f51f29b4720 to your computer and use it in GitHub Desktop.
Save XANOZOID/93e8fe66c437da5da0354f51f29b4720 to your computer and use it in GitHub Desktop.
Godot easy and composable slopes platformer code (DEMO: https://media.giphy.com/media/iPQssOxSZ61Zm/giphy.gif )
# Please credit "Ghouly The Ghost" out of your kind heart! Check my gist page for a license.
# Includes: No slip inclines, variable angle climbing, shape agnostic implementation, slope climb up/down, smooth old school vertical collisions
# WARNING! IMPORTANT!: Relies on " Scene >> Projects Settings >> Physics 2D >> motion_fixed_enabled[x] __ on[x]"
extends KinematicBody2D
const floorCheckVector = Vector2( 0, 1 )
const normalCheckVector = Vector2( 0, -1 )
var angleThreshold = 1.39 setget set_angle_threshold
var slopeScale = tan( angleThreshold )
# A safe method for moving you vertically
func move_vertical( moveVec ):
if( moveVec.y < 0 ):
# slides you smoothly when moving upwards
var over = move( moveVec )
if( is_colliding() ):
var n = get_collision_normal()
over = n.slide( over )
move( over )
else:
# moves you vertically with ZERO slip.
if( is_colliding() ):
move( moveVec )
if( is_colliding() ): revert_motion()
else: move( moveVec )
return is_colliding()
# Takes a degree, stores as radian
func set_angle_threshold( val ):
angleThreshold = deg2rad( val )
slopeScale = tan( angleThreshold )
# Checks if on floor
func is_on_floor(): return test_move( floorCheckVector )
# Method which will move you horizontally and even up slopes
func move_horizontal( moveVec ):
var wasOnFloor = is_on_floor()
var over = move( moveVec )
if(is_colliding() ):
var n = get_collision_normal()
var angle = acos( n.dot( normalCheckVector ) )
# moves you up a slope if within your threshold
if( angle < angleThreshold ):
over = n.slide( over )
move( over )
if( !test_move( floorCheckVector ) ):
var maxMove = Vector2( 0, -get_travel().y )
if( test_move( maxMove ) ): move_vertical( maxMove )
else:
# moves against element WITHOUT vertical sliding
revert_motion()
move( over )
if( is_colliding() ): revert_motion()
elif( wasOnFloor ):
# moves you down a slope if ther eis one and if you were on the floor
if( !test_move( floorCheckVector ) ):
var maxMove = Vector2( 0, get_travel().abs().x * slopeScale )
if( test_move( maxMove ) ): move_vertical( maxMove )
# Please credit "Ghouly the Ghost", out of your kind heart! Check my gist page for a license.
extends "res://KinematicPlatformer2d.gd"
var gravity = 50
var walkSpeed = 255
var jumpSpeed = -10
var velocityY = Vector2()
var velocityX = Vector2()
func _ready():
self.angleThreshold = 33 # works only with "self." <- GDScript bug?
set_fixed_process( true )
func _fixed_process( delta ):
velocityY.y += gravity * delta
if( Input.is_key_pressed( KEY_W ) ):
velocityY.y = jumpSpeed
if( move_vertical( velocityY ) ): velocityY.y = 0
if( Input.is_key_pressed( KEY_A ) ):
velocityX.x = -walkSpeed * delta
move_horizontal( velocityX )
elif( Input.is_key_pressed( KEY_D ) ):
velocityX.x = walkSpeed * delta
move_horizontal( velocityX )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment