Godot Version
4
Question
Hello,
I’m trying Godot for few days and I’m stuck with a diagonal jump issue.
My character is moving fine in every direction and can jump too, but not in every case :
Up + jump : ok
Down + jump : ok
Left + jump : ok
Right + jump : ok
Up + Right + jump : ok
Up + Down + jump : doesn’t work
Up + Left + jump : doesn’t work
Down + Left + jump : doesn’t work
Down + Right + jump : doesn’t work
Left + Right + jump : ok
Here is my code :
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 12
var xform : Transform3D
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("left", "right", "up", "down")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if input_dir != Vector2(0,0):
$MeshInstance3D.rotation_degrees.y = $Camera_Controller.rotation_degrees.y - rad_to_deg(input_dir.angle()) - 90
if is_on_floor():
align_with_floor($RayCast3D.get_collision_normal())
global_transform = global_transform.interpolate_with(xform,0.3)
elif not is_on_floor():
align_with_floor(Vector3.UP)
global_transform = global_transform.interpolate_with(xform,0.3)
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
move_and_slide()
$Camera_Controller.position = lerp($Camera_Controller.position,position,0.5)
func align_with_floor(floor_normal):
xform = global_transform
xform.basis.y = floor_normal
xform.basis.x = -xform.basis.z.cross(floor_normal)
xform.basis = xform.basis.orthonormalized()
func _on_fall_zone_body_entered(body: Node3D) -> void:
get_tree().change_scene_to_file(get_tree().current_scene.scene_file_path)# Replace with function body.
Thanks a lot !