Godot Version
4.5.1
Question
I have been trying to create a 2d character that can rotate along slopes and walls but with my code there seems to be a bug where the player starts to jitter and bug out? And im trying to see if anyone can help me figure out what i could be doing wrong
extends CharacterBody2D
var direction = 0
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
var input = Vector2.ZERO
#ALL JUMP VARIABLES
var jump_height : float = 100.0
var jump_time_to_peak : float = 0.4
var jump_time_to_descent : float = 0.25
var jump_velocity = ((2.0 * jump_height) / jump_time_to_peak) * -1.0
var jump_gravity = ((-2.5 * jump_height) / (jump_time_to_peak * jump_time_to_peak)) * -1.0
var fall_gravity =((-2.5 * jump_height) / (jump_time_to_descent * jump_time_to_descent)) * -1.0
var current_speed : float = 300
var max_speed : float = 600
var current_friction = 200
var current_accel = 200
var max_fall_speed : float = 1400
var slope_angle := 0.0 #The exact angle of a slope (Radians)
var slope_factor := 0.0 #The steepness of slopes
var rot := 0.0 #Rotation
func _physics_process(delta: float) -> void:
direction = Input.get_axis("left","right")
if not is_on_floor():
velocity.y += gravity() * delta
velocity.y = clamp(velocity.y, -max_fall_speed, max_fall_speed)
if is_on_floor():
##CAUSING PROBLEMS WITH COLLISIONS
slope_angle = get_floor_normal().angle() + (PI / 2)
slope_factor = get_floor_normal().x
else:
slope_factor = 0
#SPRITE AND COLLISION ROTATION
$".".rotation = rot
##Rotates the collisions and the sprites along slopes
if is_on_floor():
up_direction = get_floor_normal()
rot = slope_angle
else:
if !$floorchecker.is_colliding():
rot = 0
up_direction = Vector2(0, -1)
if Input.is_action_just_pressed("jump") and is_on_floor():
handle_jump()
get_input()
movement(delta)
func gravity() -> float:
return jump_gravity if velocity.y <= 0.0 else fall_gravity
func get_input():
input.x = int(Input.is_action_pressed("right")) - int(Input.is_action_pressed("left"))
return input.normalized()
func movement(delta):
input = get_input()
if input == Vector2.ZERO:
if abs(velocity.x) > (current_friction * delta):
velocity.x -= sign(velocity.x) * (current_friction * delta)
else:
velocity.x = 0
else:
velocity.x += input.x * current_accel * delta
velocity.x = clamp(velocity.x, -max_speed, max_speed)
move_and_slide()
func handle_jump():
if is_on_floor():
velocity.y = jump_velocity
Thank You