Jump being weird

Godot Version

4.4.1

Question

How do I get the jump to stop being weird?

extends CharacterBody2D

var motion := Vector2(0, 0)
var rot := 0.0

var grounded := false

var slopeangle := 0.0
var slopefactor := 0.0

var falloffwall = false
var control_lock = false
var stuck = false

var jumping = false
var canjump = true
var jumpbuffered = false
var jumptime = 0.0
const maxjumptime = 2
const JUMP_VELOCITY = 600.00
var GRAVITY = 1200.00

const acc := 4
const dec := 30.0
const topspeed := 600.0

func _physics_process(delta):

Set the Slope variables

if is_on_floor():
	jumptime = 0.0
	slopeangle = get_floor_normal().angle() + (PI/2)
	slopefactor = get_floor_normal().x
	
else:
	slopefactor = 0

Rotation & Momentum Conversion

$Collision.rotation = rot
$Sprite.rotation = lerp_angle($Sprite.rotation, rot, 0.25)

if is_on_floor():
	if not grounded:
		if abs(slopeangle) >= 0.25 and abs(motion.y) > abs(motion.x):
			motion.x += motion.y * slopefactor
		grounded = true
	
	up_direction = get_floor_normal()
	rot = slopeangle
else:
	if not $Collision/Raycast.is_colliding() and grounded:
		grounded = false
		
		motion = get_real_velocity()
		rot = 0
		up_direction = Vector2(0, -1)

Gravity

if not is_on_floor() and rot == 0:
	motion.y += GRAVITY * delta
else:
	if abs(slopefactor) == 1:
		motion.y = 0
	else:
		motion.y = 50

Jump

if Input.is_action_just_pressed("Jump"):
	if canjump:
		jumpbuffered = true
		$JumpBufferTimer.start()
if not is_on_floor() and jumping:
	if Input.is_action_pressed("Jump"):
		motion.y += GRAVITY * delta * 0.5 

if not grounded and canjump:
	if $CoyoteTimer.is_stopped():
		$CoyoteTimer.start()
else:
	$CoyoteTimer.stop()
if not is_on_floor():
	canjump = false
if jumpbuffered and canjump and not $JumpBufferTimer.is_stopped() and $CoyoteTimer.is_stopped():
	motion.y = -JUMP_VELOCITY
	jumping = true
	canjump = false
	jumpbuffered = false
	$JumpBufferTimer.stop()
	
	if abs(rot) > 1:
		position += Vector2(0, -(14)).rotated(rot)

	
	$JumpBufferTimer.stop()
	jumpbuffered = false



if motion.y >= 0 and grounded:
	jumping = false
	canjump = true


if not jumping and motion.y < -JUMP_VELOCITY / 1.625:
	if not Input.is_action_pressed("Jump"):
		motion.y = -JUMP_VELOCITY / 1.625

(Debug) Speed Boost

#if Input.is_action_just_pressed("action"):
#	motion.x += topspeed * sign($Sprite.scale.x)

Movement

var direction = Input.get_axis("Left", "Right")

if direction and not control_lock:
	if is_on_floor():
		if direction == sign(motion.x):
			if abs(motion.x) <= topspeed:
				motion.x += acc * direction
		else:
			if abs(slopefactor) < 0.4:
				motion.x += dec * direction
			else:
				motion.x += acc * direction
			
	else:
		if direction == sign(motion.x):
			if abs(motion.x) <= topspeed:
				motion.x += (acc * 2) * direction
		else:
			motion.x += (acc * 2) * direction
		
else:
	if is_on_floor() and abs(slopefactor) < 0.25:
		motion.x = move_toward(motion.x, 0, acc)

Set Velocity to the Motion variable, but rotated.

velocity = Vector2(motion.x, motion.y).rotated(rot)

Slopes

if is_on_floor() and not stuck and not $Collision/WallCast.is_colliding():
	motion.x += (acc * 2) * slopefactor

if grounded and abs(slopefactor) >= 0.5 and abs(motion.x) < 10:
	control_lock = true
	$ControlLockTimer.start()

if grounded and abs(slopeangle) > 1.5:
	if abs(motion.x) < 80:
		falloffwall = true
		position += Vector2(0, -(14)).rotated(rot)
		canjump = false
		
		control_lock = true
		$ControlLockTimer.start()
else:
	falloffwall = false

Stoppers

if is_on_ceiling() and not grounded:
	if motion.y < 0:
		motion.y = 100

if is_on_wall() and $Collision/WallCast.is_colliding():
	motion.x = 0



#slope_failsafe()
move_and_slide()

The jump velocity is super weird. I’ve been trying for hours to get a sweet spot. But either the jump is pathetic and barely goes up or the character goes into orbit. It’s annoying me because I don’t understand

Hi, can you edit your post to have proper formatting? You can use three backticks around your code to format it properly, like this:

```
code goes here
```

which turns into this:

code goes here

Anyway, you did not describe how you want the jump to work, or link anything demonstrating it, so I’m just guessing what you want to do. Can you describe how you want the jump to work? It looks like you have coyote time, but also when you press the jump button in the air, gravity is applied?

Well if it helps, my game is inspired a lot by Sonic. I plan to make it like a 2d Souls meets Sonic kind of thing. It’s just very early in development so I’m only working on getting the movement right currently.

Essentially, the issue I’m having is every value I set it to either has been short and not high at all, or the character jumps into orbit and it’s way too high

You have a lot going on in the code. Have you tried making a simpler version of the movement and trying to get the jumping values right in that version instead?

It looks like at some point you just modify the position in the code directly instead of only using move_and_slide() and you have a lot of “magic numbers” that will affect the physics.