How to steer VehicleBody towards a target?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sorcierlegrand

I’m trying to make a VehicleBody ( ) move to a target using a script. The class uses the property “steering” to get a direction, so I just used:

var fwd = car.velocity.normalized()

var target_vector = (steering_target.get_global_transform().origin -


var distance = target_vector.length()

var steer =

fwd just gives the forward direction, and is working as intended, and the rest gets the direction to the target and uses a dot product to get the cosine of the angle. Because steering should be between -1 and 1 this is fine and I don’t even bother with acos.

But this isn’t working, the car always turns to the same direction, no matter what side (left or right) the target is.

Any one tried anything similar? Any ideas on what could be? There’s a better way to steer a VehicleBody towards a target?

:bust_in_silhouette: Reply From: bloodsign

In my case, direction is determined using look_at function

var velocity = Vector2.ZERO
var ACCELERATION = Vector2(0,-1)
export(NodePath) var target

func _physics_process(delta):
 if Input.is_action_pressed("up"):
  velocity+= ACCELERATION.rotated(rotation)

:bust_in_silhouette: Reply From: ivailoburov

I tried your code and made a small change. This works for me:

func follow_target(steering_target):
var fwd = self.linear_velocity.normalized()
var target_vector = (steering_target.get_global_transform().origin - self.get_global_transform().origin)
#var distance = target_vector.length()
steering = fwd.cross(target_vector.normalized()).y
print("steering: " + String(steering))