גיבור: חזרה להתחלה
חלק מהיחידה קלט ותנועה במסלול ה-Godot של Coddy. שיעור 27 מתוך 28.
אם הגיבור נופל לבור, הוא אמור לחזור לנקודת ההתחלה.
שמרו את נקודת ההתחלה במשתנה בתוך _ready(), שרצה לפני שהגיבור זז:
var start_position = Vector2()
func _ready():
start_position = positionאחרי move_and_slide(), בדקו אם הגיבור נפל מתחת למסך שגובהו 360 פיקסלים. החזירו אותו לנקודת ההתחלה ואפסו את המהירות שלו כדי שלא יגיע כשהוא עדיין נופל:
if position.y > 400:
position = start_position
velocity = Vector2(0, 0)אתגר
בינונישלח את הגיבור בחזרה להתחלה כשהוא נופל לבור.
- הוסף
var start_position = Vector2(), ובפונקציה חדשה_ready()הגדר אותו ל־position. - אחרי
move_and_slide(), כאשרposition.y > 400, הגדר אתpositionל־start_positionואתvelocityל־Vector2(0, 0).
לחץ על ״שחק״ ונפל לבור: הגיבור חוזר להתחלה. לאחר מכן לחץ על ״בדיקה״.
נסו בעצמכם
extends CharacterBody2D
var speed = 220
var acceleration = 1200
var friction = 2400
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
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)
if direction > 0:
$Look.scale.x = 1
if direction < 0:
$Look.scale.x = -1
move_and_slide()
כל השיעורים ביחידה קלט ותנועה
5פרויקט: גיבור במשחק פלטפורמה
גיבור: ריצהגיבור: קפיצהתרגלו בעצמכם: Godot אונליין