0 votes

I'm trying to make a VehicleBody (https://docs.godotengine.org/en/stable/classes/class_vehiclebody.htm ) 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 -

    car.get_global_transform().origin)

var distance = target_vector.length()

var steer = fwd.dot(target_vector.normalized())

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?

Godot version 3.2.4.beta6
in Engine by (25 points)

2 Answers

0 votes

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)

 look_at(target.global_position)
by (334 points)
0 votes

Hi,
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))
by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.