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
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.
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")
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")
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")
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.
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!
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():