Godot Version
godotv4.3
I’ve made this code for a character, because I want him to move with small jumps that you can charge. But for some reason, it only work properly with the space bar(jump) and the up arrow(make him move following positive y). It does work with the other arrow keys, but not properly. I need to know if this is due to my code (because i know it is quite messy) or something else, because i really don’t know why it doesn’t work.
here is my code:
extends CharacterBody3D
@onready var charge = $Charge
const SPEED = 5.0
const JUMP_VELOCITY = 3.0
func _ready():
charge.value = 0
func _physics_process(delta: float) → void:
charge.visible = false
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
_movement()
move_and_slide()
func _movement():
if Input.is_action_pressed(“ui_accept”) and is_on_floor():
charge.value += 3
charge.visible = true
if Input.is_action_just_released(“ui_accept”) and is_on_floor():
velocity.y = JUMP_VELOCITY * charge.value * 0.02
charge.value = 0
charge.visible = false
if Input.is_action_pressed(“ui_left”) and is_on_floor():
charge.visible = true
charge.value += 1.5
if Input.is_action_just_released(“ui_left”) and is_on_floor():
velocity.y = JUMP_VELOCITY * charge.value * 0.02
velocity.x = SPEED * charge.value * -0.015
charge.value = 0
charge.visible = false
if Input.is_action_pressed(“ui_down”) and is_on_floor():
charge.visible = true
charge.value += 1.5
if Input.is_action_just_released(“ui_down”) and is_on_floor():
velocity.y = JUMP_VELOCITY * charge.value * 0.02
velocity.z = SPEED * charge.value * 0.015
charge.value = 0
charge.visible = false
if Input.is_action_pressed(“ui_right”) and is_on_floor():
charge.visible = true
charge.value += 1.5
if Input.is_action_just_released(“ui_right”) and is_on_floor():
velocity.y = JUMP_VELOCITY * charge.value * 0.02
velocity.x = SPEED * charge.value * 0.015
charge.value = 0
charge.visible = false
if Input.is_action_pressed(“ui_up”) and is_on_floor():
charge.visible = true
charge.value += 1.5
if Input.is_action_just_released(“ui_up”) and is_on_floor():
velocity.y = JUMP_VELOCITY * charge.value * 0.02
velocity.z = SPEED * charge.value * -0.015
charge.value = 0
charge.visible = false
elif is_on_floor():
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
pass