Godot Version
4.1.3
Question
So, I’m trying to make a game that is somewhat of a cross between the classic Sonic games and ULTRAKILL, and I’ve been working for a long time on trying to get the running on walls part down. I’ve scrapped and remade the character movement multiple times because of various issues. Currently, I’m stuck with the issue of the character getting snagged on downward slopes. It’s bizarre, the character will just lightly bounce off of a slope while going downwards on it. I have absolutely no idea what could be wrong.
This is the code for the character (incase there’s just something horribly wrong with my coding):
###########################################################
extends CharacterBody2D
####################################################################################################
####Instantiations########################################################################################################################################################
###Signals################################################################################################################################################################
signal Grounded()
###Variables##############################################################################################################################################################
##Physics
#Velocity
var runVelocityChange: Vector2 #How the character's velocity changes per-tick (With regard to running)
var jumpVelocityChange: Vector2 #How the character's velocity changes per-tick (With regard to jumping)
var velocityChange: Vector2 #How the character's velocity changes per-tick (misc)
#Forces
var currGrabity: float #The strength of the current gravity field
var normalGrabity = 20 #The strength of gravity normally
##Character
#Jumps
var jumpStrength = 600 #Force of a jump
var flaps = 4 #Doublejumps remaining
var flapsMax = 4 #Maximum doublejumps
#Horizontal Movement
var currAcceleration: float
var currAccelMod: float
var currBaseAccel: float
var accelNorm = 18 #Normal running acceleration
var accelBrakeMod = 5 #Acceleration modifier when character is running against momentum
var accelAirMod = 0.3 #Acceleration modifier when character is airborne
var maxSpeed = 1000 #Maximum speed normal running will accelerate the character to
var currDrag: float #groundDrag or airDrag
var groundDrag = 24 #Amount of drag whilst grounded
var airDrag = 3 #Amount of drag whilst airborne
var direction = 1 #Left or Right, -1 or 1
var axisInput = 0
##Misc
#Environment Conditions
var isAgainstWall = false #Whether or not the character is touching the wall
var wallSide = 0 #The side of the character a wall it touching; -1, 0 ,1 ==> left, center, right
var nullDrag = false #Whether or not drag is currently irrelevant
##########################################################################################################################################################################
####Functions#############################################################################################################################################################
###Signals################################################################################################################################################################
##Angle Detection#########################################################################################################################################################
func GroundAngleFound(Angle):
self.rotation_degrees = (-convertVector2ToDegree(Angle) + 90)
##Wall Detection##########################################################################################################################################################
func WallDetected(): #What to do if the Wall Detector is triggered
isAgainstWall = true
func NoWallDetected(): #What to do if the Wall Detector is not triggered
isAgainstWall = false
###Calculations###########################################################################################################################################################
##General Maf
func averageVector2(vector: Vector2) -> float: #Averages the X and Y values of a Vector2
var average
average = ((vector.x + vector.y) / 2)
return average
##Conversions
func convertDegreeToVector2(degree: float) -> Vector2:
var radian = 0.000
var vector = Vector2()
radian = (degree * (PI / 180))
vector.x = cos(radian)
vector.y = sin(radian)
return vector
func convertRadianToVector2(radian: float) -> Vector2:
var vector = Vector2()
vector.x = cos(radian)
vector.y = sin(radian)
return vector
func convertVector2ToRadian(vector: Vector2) -> float:
var radian
radian = acos(vector.x)
return radian
func convertVector2ToDegree(vector: Vector2) -> float:
var radian
var degree
radian = acos(vector.x)
degree = (radian / (PI / 180))
return degree
func convertRadianToDegree(radian: float) -> float:
var degree
degree = (radian / (PI / 180))
return degree
func convertDegreeToRadian(degree: float) -> float:
var radian
radian = (degree * (PI / 180))
return radian
###Misc
##_ready & _process
func _ready(): ####_ready
self.rotation_degrees = 0
func _physics_process(_delta): ####_physics_process#######################################################################################################################
###Getting Ready
#Getting Direction/Axis Input
if (Input.get_axis("ui_left", "ui_right") != 0): #If the left right axis is not 0
direction = Input.get_axis("ui_left", "ui_right") #Changes direction to the left right axis
axisInput = Input.get_axis("ui_left", "ui_right")
#Resetting Velocity Change
runVelocityChange = Vector2(0, 0) #Gets rid of velocity change from the last tick
jumpVelocityChange = Vector2(0, 0) #Gets rid of velocity change from the last tick
velocityChange = Vector2(0, 0) #Gets rid of velocity change from the last tick
#Resetting Jumps Left
if (self.is_on_floor() == true):
flaps = flapsMax
#Deciding Drag
if (self.is_on_floor() == true):
currDrag = groundDrag
else:
currDrag = airDrag
#Deciding Gravity
currGrabity = normalGrabity
#Getting Angle
if (self.is_on_floor() == true):
Grounded.emit()
else:
self.rotation_degrees = 0
###Physics and Movement###############################################################################################################################################
##Forces##############################################################################################################################################################
#Grabity##############################################################################################################################################################
if (self.is_on_floor() == false): #Checks if character is on the floor
velocityChange.y = (velocityChange.y + currGrabity) #Adds downward force to velocityChange if the character is not on the floor
elif (sign(velocity.y) == 1): #If the character is on the floor
velocityChange.y = (velocityChange.y - velocity.y) #Removes downward force from velocity if the character is on the floor
##Input###############################################################################################################################################################
#Handling Jumps#######################################################################################################################################################
if ((Input.is_action_just_pressed("ui_accept") == true) && (flaps > 0)): #Checks if "Jump" was just pressed AND if the Character has any jumps left
#Checks if the character is on the floor
if (self.is_on_floor() == false): #If character is airborne
flaps = (flaps - 1) #Removes a jump if the character is airborne
#Checks if momentum is upward or downward while airborne
if (sign(velocity.y) == 1): #If momentum is downward
jumpVelocityChange.y = ((-jumpStrength * (0.5 + (0.5 * (flaps / flapsMax)))) - velocity.y) #Negates all downward force as well as applying an upward force that weakens with each consecutive jump
else: #If momentum is upward
jumpVelocityChange.y = (-jumpStrength * (0.5 + (0.5 * (flaps / flapsMax)))) #Adds more upward force, weakening with each consecutive jump
else: #If the character is grounded
jumpVelocityChange.y = -jumpStrength #Applies upward force equal to jumpStrength
jumpVelocityChange = jumpVelocityChange.rotated(self.rotation)
#Handling Horizontal Inputs###########################################################################################################################################
#(Deciding the current acceleration modifier)
if (self.is_on_floor() == false): #If the character is not on the ground
currAccelMod = accelAirMod #Acceleration is cut to 30% if in the air
elif ((axisInput != sign(velocity.rotated(-self.rotation).x)) && (axisInput != 0)): #If instead the character is moving against momentum while on the ground
currAccelMod = accelBrakeMod #Acceleration is multiplied by 500%
else: #Otherwise
currAccelMod = 1 #There is no acceleration modifier
#(Setting the current base acceleration)
currBaseAccel = accelNorm
#(Setting the current acceleration)
currAcceleration = (currBaseAccel * currAccelMod)
#(Applying acceleration or drag)
if ((axisInput != 0) && (abs(velocity.rotated(-self.rotation).x) <= maxSpeed)): #If a left or right input is being made
runVelocityChange.x = (runVelocityChange.x + (currAcceleration * axisInput)) #Adds gradual horizontal force dependant upon the left or right input
elif (axisInput == 0): #If no horizontal input is being made
if (abs(velocity.x) > currDrag):
runVelocityChange.x = (runVelocityChange.x - (currDrag * sign(velocity.x))) #Gradually negates horizontal velocity when no horizontal input is being made
else:
runVelocityChange.x = (runVelocityChange.x - velocity.x)
runVelocityChange = runVelocityChange.rotated(self.rotation)
##Angle Handling######################################################################################################################################################
##Finalizing##########################################################################################################################################################
velocity = ((velocity + velocityChange) + (jumpVelocityChange + runVelocityChange))
print("----------")
print("is on floor? ", self.is_on_floor())
print("velocity ", velocity)
print("rotation ", self.rotation_degrees)
print("position ", self.transform)
print("currAccel ", currAcceleration)
move_and_slide()
###########################################################
Also, the character has circular collision and there’s a raycast downwards to get the collision normal of the floor
Any help would be appreciated, thanks!