영웅: 시작 지점으로 돌아가기
Coddy Godot 여정의 입력과 이동 섹션에 포함된 레슨. 28개 중 27번째.
영웅이 구덩이에 빠지면 시작 위치로 돌아가야 합니다.
영웅이 움직이기 전에 실행되는 _ready()에서 시작 위치를 변수에 저장합니다.
var start_position = Vector2()
func _ready():
start_position = positionmove_and_slide() 뒤에 영웅이 360픽셀 화면 아래로 떨어졌는지 확인합니다. 영웅을 시작 위치로 되돌리고, 계속 떨어지는 상태로 도착하지 않도록 속도를 초기화합니다.
if position.y > 400:
position = start_position
velocity = Vector2(0, 0)챌린지
중급_ready()에서 시작 위치를 저장합니다. 영웅이 y = 400 아래로 떨어지면 그 위치로 되돌리고 속도를 Vector2(0, 0)으로 설정합니다.
직접 해보기
extends CharacterBody2D
var speed = 220
var acceleration = 1200
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)
if direction > 0:
$Look.scale.x = 1
if direction < 0:
$Look.scale.x = -1
move_and_slide()
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
입력과 이동의 모든 레슨
직접 연습해 보세요: Godot 플레이그라운드