총알 발사하기
Coddy Godot 여정의 프로젝트 섹션에 포함된 레슨. 36개 중 14번째.
E 또는 Enter(action 키)를 누르면 총알이 발사됩니다. 총알은 bullet.tscn이라는 씬입니다. 총알의 자체 스크립트가 총알을 앞으로 날아가게 하고 0.8초 후에 제거합니다. 우주선은 기수에서 총알을 하나 만들고, 우주선과 같은 방향을 향하게 한 다음 게임에 추가합니다.
if Input.is_action_just_pressed("action"):
var bullet = BULLET.instantiate()
bullet.position = position + Vector2.UP.rotated(rotation) * 18
bullet.rotation = rotation
get_parent().add_child(bullet)총알을 우주선의 부모에 추가하세요. 자식은 부모와 함께 움직이고 회전하므로, 우주선의 자식으로 총알을 두면 우주선과 함께 빙글빙글 돕니다.
is_action_just_pressed는 키를 누른 프레임에만 true입니다. 누를 때마다 총알 하나가 발사됩니다.
챌린지
중급E 키를 눌러 총알을 발사하세요.
const BULLET = preload("res://bullet.tscn")을 추가하세요.action을 막 눌렀을 때, 우주선의rotation을 적용해 우주선 앞쪽 18픽셀 위치에 총알을 만드세요.get_parent().add_child(bullet)을 사용해 우주선의 부모에 총알을 추가하세요.
‘플레이’를 누른 다음 E를 누르세요. 우주선 앞부분에서 총알이 날아갑니다. 그런 다음 ‘확인’을 누르세요.
직접 해보기
extends CharacterBody2D
const TURN_SPEED = 4.0
const THRUST = 300
func _physics_process(delta):
rotation += Input.get_axis("move_left", "move_right") * TURN_SPEED * delta
if Input.is_action_pressed("move_up"):
velocity += Vector2.UP.rotated(rotation) * THRUST * delta
$Flame.visible = Input.is_action_pressed("move_up")
position += velocity * delta
if position.x < 0:
position.x += 640
if position.x > 640:
position.x -= 640
if position.y < 0:
position.y += 360
if position.y > 360:
position.y -= 360
프로젝트의 모든 레슨
2프로젝트: 소행성 슈팅 게임
우주선 회전하기엔진 가동하기직접 연습해 보세요: Godot 플레이그라운드