움직이는 방향 바라보기
Coddy Godot 여정의 입력과 이동 섹션에 포함된 레슨. 28개 중 20번째.
영웅은 오른쪽을 바라보도록 그려져 있으며, 왼쪽으로 달릴 때도 여전히 오른쪽을 바라봅니다.
영웅에서 보이는 모든 것은 Look 자식에 있습니다. scale.x를 -1로 설정하면 노드와 자식 노드가 좌우로 뒤집힙니다. 그러니 Look을 뒤집으세요.
if direction > 0:
$Look.scale.x = 1
if direction < 0:
$Look.scale.x = -1CharacterBody2D 자체는 뒤집지 마세요. 그래야 충돌 모양이 그대로 유지됩니다. 키를 누르지 않으면 direction은 0이므로 어느 if도 실행되지 않습니다. 영웅은 마지막으로 이동한 방향을 계속 바라봅니다.
챌린지
쉬움영웅이 달리는 방향을 바라보게 하세요. 왼쪽으로 달릴 때는 $Look.scale.x를 -1로, 오른쪽으로 달릴 때는 1로 설정하세요. 멈추면 같은 방향을 계속 바라봅니다.
직접 해보기
extends CharacterBody2D
var speed = 220
var acceleration = 900
var friction = 2400
var gravity = 1000
var jump_velocity = -500
func _physics_process(delta):
velocity.y += gravity * delta
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_velocity
var direction = Input.get_axis("move_left", "move_right")
if direction == 0:
velocity.x = move_toward(velocity.x, 0, friction * delta)
else:
velocity.x = move_toward(velocity.x, direction * speed, acceleration * delta)
move_and_slide()
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
입력과 이동의 모든 레슨
직접 연습해 보세요: Godot 플레이그라운드