All that is left is the (jump) for my first little game!

Godot Version

Godot_v4.3-stable

Question

my player can walk, crouch, shoot but I can’t jump!
I am new to all this, and I have tried many things to make my character jump but no success, can someone tell me what is wrong in the script and how to fix this and make my player jump?

here is the script

extends CharacterBody2D

const SPEED = 180
const JUMPFORCE = -950
const GRAVITY = 40

@export var speed = 200
@export var bullet_scene : PackedScene
@onready var sprite_2d: AnimatedSprite2D = $Sprite2d



func get_input():
	
	var dir = Input.get_axis("back", "forward")
	velocity = transform.x * dir * speed
	if Input.is_action_just_pressed("shoot"):
		shoot()
	
	
func _physics_process(delta):
	get_input()
	# Animations
	if Input.is_action_just_pressed("crouch"):
		sprite_2d.animation = "crouch"
	if Input.is_action_just_released("crouch"):
		sprite_2d.animation = "default"
		
	if Input.is_action_just_pressed("back"):
		velocity.x = SPEED
		sprite_2d.animation = "back"
	if Input.is_action_just_released("back"):
		sprite_2d.animation = "default"
		
		
	if Input.is_action_just_pressed("forward"):
		velocity.x = SPEED
		sprite_2d.animation = "forward"
	if Input.is_action_just_released("forward"):
		sprite_2d.animation = "default"
		
		
		
	if Input.is_action_just_pressed("jump")and is_on_floor():
		velocity.y = JUMPFORCE
		$Sprite2d.play("jump")
	
	if Input.is_action_just_released("jump"): 
		sprite_2d.animation = "default"
		
	velocity.y+=GRAVITY
	move_and_slide()
	
	velocity.x=lerp(velocity.x,0.0,0.1)
	
func shoot():
	var b = bullet_scene.instantiate()
	get_tree().root.add_child(b)
	b.transform = $Muzzle.global_transform

If you put three backticks (```) above and below your script it’ll format more nicely.

What is the jump doing right now? With a force of -950, I’d kind of expect it to launch like a rocket…

1 Like

The jump line has no space between the “)” and the “and”, which could be causing problems.
I’d start with having it print a line when you jump, just to make sure its processing correctly. No print = error in if-then. Print = error with jump code.

1 Like

the jump is doing nothing right now when I press the key
I really want to fix this and make the player jump

I tried const JUMPFORCE = -100

nothing happens

Are you sure you are on the floor?

Try adding this to check.

print(is_on_floor())

Also check your “jump” is defined in the input map and it is not, say, “Jump”.

Hope that helps.

if Input.is_action_just_pressed("jump")and is_on_floor():
	print("Jump button recognised")
	print("Is on floor is bool: ", is_on_floor())
	print("Jump Force is: ", JUMPFORCE)
	velocity.y = JUMPFORCE
	$Sprite2d.play("jump")
1 Like

in the input map it’s “jump”
and the animation name is jump

I tried this

if Input.is_action_just_pressed("jump")and is_on_floor():
	print("Jump button recognised")
	print("Is on floor is bool: ", is_on_floor())
	print("Jump Force is: ", JUMPFORCE)
	velocity.y = JUMPFORCE
	$Sprite2d.play("jump")

but still can’t jump

What gets printed?

Also try it this way around:

print("Is on floor is bool: ", is_on_floor())
if Input.is_action_just_pressed("jump")and is_on_floor():
	print("Jump button recognised")
	print("Jump Force is: ", JUMPFORCE)
	velocity.y = JUMPFORCE
	$Sprite2d.play("jump")
1 Like

in the Output
I get this

" Is on floor is bool: false "

where exactly should I put that code in the script

I changed to this

	print("Is on floor is bool: ", is_on_floor())
	if Input.is_action_just_pressed("jump")and is_on_floor():
		print("Jump button recognised")
		print("JUMP VELOCITY is: ", JUMP_VELOCITY)
		velocity.y = JUMP_VELOCITY
		sprite_2d.animation = ("jump")

then I get

" Is on floor is bool: false "

So the reason your jumping is not working is because your is_on_floor test is failing.

if Input.is_action_just_pressed("jump") and is_on_floor():
       # the rest is never called because you are not on the floor

If you look here in the docs:

Hopefully you may be able to find the cause of this in your setup.

The up_direction and floor_max_angle are used to determine whether a surface is “floor” or not.

You need to have defined the collision areas correctly for the floor and the up_direction (there are links to these in the docs I listed above).

Hope that helps.

PS As a quick test, remove the ‘and is_on_floor()’ and your jump will probably work. Except you will be able to jump when in the air too.

1 Like

I have noticed my player (characterbody2d has no physics he can’t fall too just floating wherever I put him, is there no simple way to make him have physics and jump / fall?

I may not understand what I should do from the link you posted, English is not my first language, and I don’t want to start from the beginning just to make my player jump with physics.

You can do:
If not is_on_floor():
velocity += get_gravity() * delta
In your physics process function to give gravity to it

2 Likes

By the way this is my preferred way to jump, like controlling how high you wanna jump. So here’s my code that might work:

If Input.is_action_just_released(“jump”):
Velocity.y *= 0.6

Put this code in the function where you added your gravity and stuff (your physics_process function)

And make sure your jump button exists, if not go to project setting and input map and add a new input

And you can make your idle animation autoplay in animatedsprite, not with code, so that it could play all the time, of course, it stops playing when another animation is being played

I would be happy to know if this worked for you or if my gravity worked!

1 Like

i get this

Hi, its get_gravity() not get_gravity, write it with parenthesis ()

1 Like

thanks! this worked for falling, but not jumping?

1 Like

Oh my bad, do this exact script

Even though you did something like this, still try this out, it might work. I think your mistake with your old jump code was that you didnt add a space when you said “and”

You did: If Input.is_action_just_pressed(“jump”)and is_on_floor():

You didnt add a space between (“jump”) and “and”

1 Like