Menu
CoddyTech

גיבור: ריצה חלקה

חלק מהיחידה קלט ותנועה במסלול ה-Godot של Coddy. שיעור 25 מתוך 28.

הגיבור מתחיל לנוע ועוצר בתוך פריים יחיד. גרמו לו להאיץ באמצעות 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. הגיבור מגיע למהירות מלאה בתוך כחמישית השנייה, כך שעדיין נשאר לו מרחק לרוץ לפני הבור.

challenge icon

אתגר

בינוני

גרום לגיבור להאיץ ולעצור בצורה חלקה.

  1. הוסף var acceleration = 1200 ו-var friction = 2400.
  2. שמור את הציר ב-var direction. כשאף מקש אינו לחוץ, השתמש ב-move_toward כדי לקרב את velocity.x ל-0 בצעדים של friction * delta.
  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 אונליין