Menu
CoddyTech

Un rival en la pista

Lección 13 de 20 del curso Juego de Godot: Circuito Neón de Coddy.

Un coche rosa espera a tu lado. Es un rival y no necesita que lo dirijas: sigue la línea central de la pista, un poco hacia un lado.

Guarda cuánto ha recorrido de la pista en progress y haz que aumente a su velocidad máxima. track.point_at(progress) es ese punto de la línea central, y direction_at indica la dirección que sigue la pista allí:

func place() -> void:
	var direction: Vector2 = track.direction_at(progress)
	position = track.point_at(progress) + direction.orthogonal() * lane
	rotation = direction.angle()

lane lo desplaza esa cantidad de píxeles respecto de la línea central, para que los rivales puedan conducir uno al lado del otro. progress sigue aumentando después de una vuelta; point_at lo hace dar la vuelta.

challenge icon

Desafío

Fácil
  1. En rival.gd, añade las exportaciones top_speed := 200.0, lane := 0.0, start_offset := 40.0.
  2. Añade var progress := 0.0, var active := true y la pista con @onready.
  3. En _ready, establece progress = start_offset y llama a place().
  4. En _physics_process, retorna si no está activo, incrementa progress en top_speed * delta y llama a place().

Pruébalo tú mismo

extends Node2D

@export var accel := 260.0
@export var max_speed := 200.0
@export var turn_speed := 3.2
@export var drag := 0.6
@export var grip := 7.0

var velocity := Vector2.ZERO
var boost_time := 0.0
var boost_charge := 0.0
var active := true

@onready var track: Node2D = $"../Track"


func _physics_process(delta: float) -> void:
	var throttle := 0.0
	var steer := 0.0
	if active:
		throttle = Input.get_axis("move_down", "move_up")
		steer = Input.get_axis("move_left", "move_right")
		if Input.is_action_just_pressed("action") and boost_charge >= 1.0:
			boost_charge = 0.0
			boost_time = 1.0
	boost_charge = minf(1.0, boost_charge + delta / 10.0)
	$"../HUD/Boost".scale.x = boost_charge

	var forward := Vector2.RIGHT.rotated(rotation)
	var speed := velocity.dot(forward)
	var top := max_speed
	var grip_now := grip
	if boost_time > 0.0:
		boost_time -= delta
		top = max_speed * 1.3
		velocity += forward * accel * delta
	for pad in get_tree().get_nodes_in_group("boost_pads"):
		if position.distance_to(pad.position) < 22.0:
			boost_time = maxf(boost_time, 0.4)
	for oil in get_tree().get_nodes_in_group("oil"):
		if position.distance_to(oil.position) < 20.0:
			grip_now = grip * 0.12
	if not track.is_on_road(position):
		top = max_speed * 0.45
		velocity *= 1.0 - 2.5 * delta

	if throttle < 0.0 and speed > 10.0:
		velocity += forward * throttle * accel * 1.6 * delta
	else:
		velocity += forward * throttle * accel * delta
	rotation += steer * turn_speed * delta * clampf(absf(speed) / 90.0, 0.0, 1.0) * signf(speed + 0.01)

	var side := forward.orthogonal()
	velocity -= side * velocity.dot(side) * minf(1.0, grip_now * delta)
	velocity *= 1.0 - drag * delta
	if velocity.length() > top:
		velocity = velocity.move_toward(velocity.limit_length(top), accel * 2.0 * delta)
	position += velocity * delta
	$Exhaust.emitting = throttle > 0.0 or boost_time > 0.0
	$Flame.visible = boost_time > 0.0

Todas las lecciones de Juego de Godot: Circuito Neón

Practica por tu cuenta: Playground de Godot