[03]プレイヤー(飛行機)のスクリプト

2Dシューティング

extends CharacterBody2D

# 移動スピード
const move_speed = 100.0
# 傾き
var turnTilt = 4

var direction: Vector2

func _ready() -> void:
	add_to_group("player")

func _physics_process(delta: float) -> void:
	direction.x = Input.get_axis("move_left", "move_right")
	direction.y = Input.get_axis("move_up", "move_down")
	
	if direction.x:
		velocity = direction.normalized() * move_speed
		if direction.x < 0:
			rotation = -1 * turnTilt * delta
		elif direction.x > 0:
			rotation = 1 * turnTilt * delta
	elif direction.y:
		velocity = direction.normalized() * move_speed
		rotation = 0 * turnTilt * delta
	else :
		velocity = Vector2.ZERO

	position += velocity * delta

	# 画面の外に行かないように	
	var viewRect = get_viewport_rect()
	var viewOffset = Vector2(50, 50)
	position = position.clamp(viewOffset, viewRect.size - viewOffset)

	move_and_slide()