Having Problem With Characterbody2d and rotation jitters

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

It might be helpful to know what the “jitter and bugging out” means more precisely, what triggers it, what possible causes you excluded already etc.

1 Like

Sorry for the late reply what I meant by jittering is the collision of the character is rapidly moving left and right over and over again


Edit: What causes it is just my movement I am trying to use a segmentshape2d as my collision shape but when its anything but a large rectangleshape2d it causes the character to do the jitter bug as shown above, and the reason I am trying to use a segment shape 2d is they work as a much better floor collision for running along slopes

It looks like your shape is being rotated by a small angle around a point somewhere above it, I guess around the center of the sprite. Since you set rotation from the get_floor_normal() it’s worth checking what its output is.