마찰
Coddy Godot 여정의 입력과 이동 섹션에 포함된 레슨. 28개 중 19번째.
대부분의 게임 캐릭터는 출발할 때보다 더 빠르게 멈춥니다. 키를 누르지 않았을 때 속도가 줄어드는 현상을 마찰이라고 합니다.
멈출 때 다른 숫자를 사용하려면 코드에 두 가지 경로가 필요합니다. else는 if에 두 번째 경로를 제공하며, 두 경로 중 정확히 하나만 실행됩니다:
if direction == 0:
velocity.x = move_toward(velocity.x, 0, friction * delta)
else:
velocity.x = move_toward(velocity.x, direction * speed, acceleration * delta)==는 두 값이 같은지 확인합니다. = 하나는 변수에 값을 할당합니다. 키를 누르지 않았을 때 direction == 0은 참입니다.
300처럼 friction이 작으면 얼음 위에서 미끄러지듯 움직입니다. 2400처럼 값이 크면 몇 프레임 만에 멈춥니다.
챌린지
중급손을 떼면 영웅이 빠르게 멈추도록 하세요.
move_toward줄을else:아래에 놓으세요.- 그 위의
if direction == 0:아래에서move_toward를 사용해friction * delta만큼velocity.x를0으로 줄이세요.
‘플레이’를 누르고 달리다가 손을 떼세요. 영웅이 조금 미끄러진 다음 멈춥니다. 그런 다음 ‘확인’을 누르세요.
직접 해보기
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")
velocity.x = move_toward(velocity.x, direction * speed, acceleration * delta)
move_and_slide()
입력과 이동의 모든 레슨
직접 연습해 보세요: Godot 플레이그라운드