followed online tots, got run walk ldle done but wen i press jump everytime i start new project it first jump couple secs up standing mid air before the jump animation kicks in and sometimes when the jumps works it only works because there are no object close by
extends CharacterBody3D
@onready var animation_player: AnimationPlayer = $man/ybot_man/AnimationPlayer
const SPEED = 5.0
const JUMP_VELOCITY = 12.0
# This "snaps" the character to the floor so animations don't flicker
var was_on_floor = false
func _physics_process(delta: float) -> void:
# 1. GRAVITY
if not is_on_floor():
velocity += get_gravity() * delta
# 2. MOVEMENT INPUT
var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
# Apply speed to X and Z only to keep Jump (Y) separate
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
# Smooth Rotation
var target_rotation = atan2(direction.x, direction.z)
$man.rotation.y = lerp_angle($man.rotation.y, target_rotation, 0.15)
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
# 3. JUMPING
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Force the jump animation to start immediately
animation_player.play("jump", 0.1)
# 4. EXECUTE
move_and_slide()
# 5. ANIMATION LOGIC (The "Flicker" Fix)
if is_on_floor():
if velocity.length() > 0.5:
if animation_player.current_animation != "run":
animation_player.play("run", 0.2)
else:
if animation_player.current_animation != "idle":
animation_player.play("idle", 0.2)
else:
# We are in the air
if animation_player.current_animation != "jump":
animation_player.play("jump", 0.1)
This stands out as a good candidate for your issue:
# 3. JUMPING
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Force the jump animation to start immediately
animation_player.play("jump", 0.1)