Car top down 2d enemy steering behavior controlled with degrees to the player charakter

Godot Version



hello !!

i hope my question is intresting enough for you to help me.
if you need more information to help answer it pls say so, i will deliver more info.

situation: im a absolute noob @ godot. i write little scripts in python ( like webscapers). i want just to experiment with diffrent types of enemys moving in groups and with the player.

right now i have a working 2d top down car from the classic gdscript tutorial.
now i want to copy the hole car including the script BUT change the conditions for this lines:

func get_input():
var turn = Input.get_axis(“steer_left”, “steer_right”)
steer_direction = turn * deg_to_rad(steering_angle)
if Input.is_action_pressed(“accelerate”):
acceleration = transform.x * engine_module.engine_power
if Input.is_action_pressed(“brake”):
acceleration = transform.x * braking

in to this:

func get_input():

    if degree to player car is betweenn -178 and -20:
     var turn = -15
     steer_direction = turn * deg_to_rad(steering_angle)
    if degree to player car is betweenn 178 and 20:
     var turn = 15
     steer_direction = turn * deg_to_rad(steering_angle)

if degree to player car is between -20 and 20:
	acceleration = transform.x * engine_module.engine_power

i dont want the enemy car to steer directly. instead i want to have conditions related to the degree it has to the player car and with this info it steers. its like a robotic car.

i assume if i read the pdf’s right the angles used in the pseudo code here are worng but its better to show what i want.

i tried to write the script part with chat gpt and microsoft edge but i get always stuck at the place where my enemy car need defined the other node for calculation.
right now the scirpt for this looks like this:

extends CharacterBody2D

@onready var storage_module = get_node("StorageModule")
@onready var engine_module = get_node("EngineModule")
@onready var electronics_module = get_node("ElectronicsModule")
@onready var cockpit_module = get_node("CockpitModule")
@onready var wheels_module = get_node("WheelsModule")
@export var player: NodePath  # NodePath für den Spieler

var wheel_base = 65
var steering_angle = 16
var braking = -400
var slip_speed = 40

var acceleration = Vector2.ZERO
var steer_direction = 0.0
var current_position = get_global_position()

func _ready():
    if player:
        player = get_node(player)

func _physics_process(delta):
    acceleration = Vector2.ZERO
    velocity += acceleration * delta

func apply_friction(delta):
    if acceleration == Vector2.ZERO and velocity.length() < 50:
        velocity = Vector2.ZERO
    var friction_force = velocity * wheels_module.friction * delta
    var drag_force = velocity * velocity.length() * wheels_module.drag * delta
    acceleration += drag_force + friction_force

func get_input():
    if player:
        var angle_to_player = get_angle_to_player()
        # Gas geben, wenn Winkel zwischen -90 und 90 Grad
        if angle_to_player >= -90 and angle_to_player <= 90:
            acceleration = transform.x * engine_module.engine_power
        # Nach links lenken, wenn Winkel zwischen -120 und 0 Grad
        if angle_to_player >= -120 and angle_to_player < 0:
            steer_direction = -deg_to_rad(steering_angle)
        # Nach rechts lenken, wenn Winkel zwischen 0 und 120 Grad
        if angle_to_player > 0 and angle_to_player <= 120:
            steer_direction = deg_to_rad(steering_angle)

func calculate_steering(delta):
    var rear_wheel = position - transform.x * wheel_base / 2.0
    var front_wheel = position + transform.x * wheel_base / 2.0
    rear_wheel += velocity * delta
    front_wheel += velocity.rotated(steer_direction) * delta
    var new_heading = rear_wheel.direction_to(front_wheel)
    var traction = wheels_module.traction_slow
    if velocity.length() > slip_speed:
        traction = wheels_module.traction_fast
    var d =
    if d > 0:
        velocity = lerp(velocity, new_heading * velocity.length(), traction * delta)
    if d < 0:
        velocity = -new_heading * min(velocity.length(), engine_module.max_speed_reverse)
    rotation = new_heading.angle()

func get_angle_to_player() -> float:
    if player:
        var player_position = player.global_position
        var direction = player_position - global_position
        var angle_to_player = direction.angle()
        var angle_to_player_degrees = rad2deg(angle_to_player)
        return angle_to_player_degrees

    return 0.0

i hope i did write a good question witch helps the community if my question has already a good answer, sorry i didnt find it, a link to there i would love to see.

Best regards btfg