Menu
CoddyTech
flag Ar iconالعربيةdown icon

البطل: التوجّه في الاتجاه

جزء من قسم الإدخال والحركة في رحلة Godot على Coddy. الدرس 26 من 28.

ما زال البطل يواجه اليمين عندما يركض إلى اليسار. اقلب عقدة Look الخاصة به بالطريقة التي تشير إليها أزرار الاتجاه:

if direction > 0:
	$Look.scale.x = 1
if direction < 0:
	$Look.scale.x = -1

ضع هذه الأسطر بعد تعيين direction. عندما لا يكون أي زر مضغوطًا، لن يُنفَّذ أيٌّ من شرطي if، لذا سيظل البطل متجهًا إلى آخر جهة واجهها.

challenge icon

التحدي

سهل

اجعل البطل يواجه الاتجاه الذي يركض نحوه عبر تعيين $Look.scale.x إلى 1 أو -1. وعندما تتوقف، يظل مواجهًا للاتجاه نفسه.

جرّب بنفسك

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)
	move_and_slide()
quiz iconاختبر نفسك

يتضمن هذا الدرس اختبارًا قصيرًا. ابدأ الدرس للإجابة عليه وتتبّع تقدمك.

جميع دروس الإدخال والحركة

تدرّب بنفسك: Playground لـ Godot