Menu
CoddyTech

조향

Coddy의 Godot 게임: 네온 서킷 코스 레슨. 20개 중 2번째.

이제 자동차가 회전합니다. Input.get_axis는 두 키를 한 번에 읽습니다. 첫 번째 키는 -1, 두 번째 키는 1, 아무 키도 누르지 않으면 0을 반환합니다.

var steer := Input.get_axis("move_left", "move_right")

실제 자동차는 움직일 때만 회전하고, 거의 움직이지 않을 때는 덜 회전합니다. 따라서 회전량은 속도에 비례합니다. velocity.dot(forward)는 자동차가 바라보는 방향으로 얼마나 빠르게 움직이는지를 나타냅니다(뒤로 움직일 때는 음수입니다).

var speed := velocity.dot(forward)
rotation += steer * turn_speed * delta * clampf(absf(speed) / 90.0, 0.0, 1.0) * signf(speed + 0.01)

clampf(absf(speed) / 90.0, 0.0, 1.0)는 정지 상태일 때 0이고 초당 90픽셀일 때 1이 됩니다. 실제 자동차처럼, signf는 뒤로 움직일 때 회전 방향을 뒤집습니다. Godot에서는 회전값이 커질수록 시계 방향으로 회전하므로 Right는 회전값을 증가시킵니다.

challenge icon

챌린지

쉬움
  1. max_speed 아래에 @export var turn_speed := 3.2를 추가하세요.
  2. throttle 아래에서 steer를 읽으세요.
  3. forward 다음에 speed를 계산하세요.
  4. 속도가 커진 다음, 위의 줄을 사용해 자동차를 회전시키세요.

직접 해보기

extends Node2D

@export var accel := 260.0
@export var max_speed := 200.0

var velocity := Vector2.ZERO


func _physics_process(delta: float) -> void:
	var throttle := Input.get_action_strength("move_up")

	var forward := Vector2.RIGHT.rotated(rotation)

	velocity += forward * throttle * accel * delta
	velocity = velocity.limit_length(max_speed)
	position += velocity * delta
	$Exhaust.emitting = throttle > 0.0

Godot 게임: 네온 서킷의 모든 레슨

직접 연습해 보세요: Godot 플레이그라운드