Godot Version
4.2.1 stable
Question
i’m making a game with a square as the character and in his animations i use a lot of positioning, rotating and scaling. All of the animations work in the game, except the jump animation, on the editor, it works just fine. but in the game, it doesn’t work
i first tried using this code:
extends CharacterBody2D
const SPEED = 450.0
const JUMP_VELOCITY = -650.0
const Acceleration = 90.0
const Friction = 50.0
@onready var spr = $Sprite2D
@onready var Anim = $AnimationPlayer
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
elif Input.is_action_just_released("ui_accept") and not velocity.y > 1:
velocity.y = velocity.y / 2
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = move_toward(velocity.x, direction * SPEED, Acceleration)
spr.flip_h = false
else:
velocity.x = move_toward(velocity.x, 0, Friction)
if direction < 0:
spr.flip_h = true
Handle_Animation(direction)
move_and_slide()
func Handle_Animation(direction):
if direction:
Anim.play("Run")
else:
Anim.play("Idle")
if not is_on_floor():
Anim.play("Jump")```
and then this code:
extends CharacterBody2D
const SPEED = 450.0
const JUMP_VELOCITY = -650.0
const Acceleration = 90.0
const Friction = 50.0
@onready var spr = $Sprite2D
@onready var Anim = $AnimationPlayer
Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
elif Input.is_action_just_released("ui_accept") and not velocity.y > 1:
velocity.y = velocity.y / 2
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = move_toward(velocity.x, direction * SPEED, Acceleration)
spr.flip_h = false
else:
velocity.x = move_toward(velocity.x, 0, Friction)
if direction < 0:
spr.flip_h = true
Handle_Animation(direction)
move_and_slide()
func Handle_Animation(direction):
if is_on_floor():
if direction:
Anim.play(“Run”)
else:
Anim.play(“Idle”)
if not is_on_floor() and not Anim.current_animation != "Jump":
Anim.play("Jump")
but nothing works