@Pushiron

Как сделать анимацию разворота?

На данном этапе имеется вот такой код Player.gd
extends CharacterBody2D


@export var speed = 100.0
@export var jump_force = 400.0
var jump_velocity = -jump_force

# 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
		$AnimationPlayer.play("Jump")

	# 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 = direction * speed

	else:
		velocity.x = move_toward(velocity.x, 0, speed)

	if direction != 0:
		switch_direction(direction)
		
	
	move_and_slide()
	
	update_animations(direction)

func switch_direction(direction):
	$Sprite2D.flip_h = (direction == -1)
	$Sprite2D.position.x = direction * 4

func update_animations(direction):
	if is_on_floor():
		if direction == 0:
			$AnimationPlayer.play("Idle")
		else:
			$AnimationPlayer.play("Run")
	else:
		if velocity.y < 0:
			$AnimationPlayer.play("Jump")
		elif  velocity.y > 0:
			$AnimationPlayer.play("Fall")


Имеется анимация "Turn". Не совсем могу сообразить как мне проиграть эту анимацию и дальше проигрывать анимацию бега и при этом еще учитывать направление взгляда игрока. Чтобы если он бежит слева на право, то анимация разворота проигрывалась так, как есть, а если слева на право, то зеркально.

Заранее спасибо за помощь!
  • Вопрос задан
  • 47 просмотров
Пригласить эксперта
Ответы на вопрос 1
SergeyFedorenko
@SergeyFedorenko
Вот пример простого скрипта, который может использоваться для анимации разворота:

extends KinematicBody2D

export(NodePath) var target_node
export(float) var speed = 500.0

func _ready():
    var animation = AnimationPlayer.new()
    animation.start("turn_to_target", target_node)
    set_process_input(true)


func process_input(delta):
    direction = target_node.global_position - get_global_position()
    if direction.length() > 1:
        rotation_degrees = atan2(direction.y, direction.x)
        rotation_degrees += PI / 2
        rotation_degrees *= 180 / PI
    else:
        rotation_degrees = 0
    rotation += rotation_degrees * speed * delta

func _physics_process(delta):
    var velocity = direction.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы