Menu
CoddyTech

主人公:滑らかに走る

CoddyのGodotジャーニー「入力と移動」セクションの一部。レッスン 25/28。

主人公は1フレームで動き始め、止まります。accelerationで加速し、frictionで停止するようにしましょう:

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)

accelerationには1200、frictionには2400を使います。主人公は約5分の1秒で最高速度に達するため、穴の手前まで走る余裕があります。

challenge icon

チャレンジ

中級

ヒーローが滑らかに加速して止まるようにしましょう。

  1. var acceleration = 1200 と var friction = 2400 を追加します。
  2. 軸の値を var direction に格納します。キーが押されていないときは、move_toward を使って velocity.x を friction * delta ずつ 0 に近づけます。
  3. キーが押されているときは、move_toward を使って direction * speed に acceleration * delta ずつ近づけます。

「プレイ」を押します。ヒーローが加速し、止まると少し滑って、落とし穴を越えます。その後、「確認」を押します。

自分で試してみよう

extends CharacterBody2D

var speed = 220
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
	velocity.x = Input.get_axis("move_left", "move_right") * speed
	move_and_slide()

入力と移動のすべてのレッスン

自分で練習してみよう: Godotプレイグラウンド