Vehiclebody look_at is backwards

Godot Version

4.2.2

Question

In my racing game, I am trying to program in a racing opponent. For this I am using a vehicle body that navigates using Godot’s pathfinding system. It uses the pathfinding system by using the look_at() function to look at the next path position while moving forward using engine force. However, I have ran into a problem. That is, the vehicle seems to look at the next given position backwards. This makes it go in the opposite direction of the next position

In this image, you can see the red AI car to the left of the player. As you can see, it is on a red path. The capsule sticking out of the vehicle marks where the front should be. To get the vehicle to navigate through the course, I have to make the engine force negative, making the vehicle drive backwards, as seen in the image.

My code for the enemy is below (its a work in progress):

extends VehicleBody3D

const ACCELERATION: float = 50.0
var is_getting_boost: bool = false

var speed: float = 0
var max_speed: float = 200.0

var checkpoint_positions = []
var checkpoint_pos_index = 6

var possible_stuck_pos = position

@onready var last_checkpoint_position = global_position
@onready var last_checkpoint_rotation = global_rotation

var checkpoints_reached = []

func _ready():
	
	# Stores the global positions of all cehckpoints in the level
	for checkpoint in get_tree().get_nodes_in_group("Checkpoints"):
		
		checkpoint_positions.append(checkpoint.global_position)
	
	await Engine.get_main_loop().process_frame
	$NavigationAgent3D.target_position = checkpoint_positions[checkpoint_pos_index]
		
func _physics_process(delta):
	
	# Once the next checkpoint is reached, the vehicle is ordered to go to the next checkpoint
	if global_position.distance_to($NavigationAgent3D.target_position) < 1:
		checkpoint_pos_index += 1
		
		if checkpoint_pos_index > 6:
			
			checkpoint_pos_index = 0
		$NavigationAgent3D.target_position = checkpoint_positions[checkpoint_pos_index]
		
	var next_pos = $NavigationAgent3D.get_next_path_position()
	if global_position != next_pos:
		
		look_at(next_pos)
	
	# If the vehicle is getting a boost from a speed pad, the engine force is maximized
	if is_getting_boost:
		
		engine_force = -max_speed
		
	else:
		
		# Speed increases by acceleration until it gets to max speed
		speed = min((speed + ACCELERATION), max_speed)
		engine_force = -speed
	
	# If vehicle goes to fast or stays still, it starts a reset check timer, b/c it may have fallen off the map or gotten stuck
	if ((linear_velocity.length() < 1) or (linear_velocity.length() > 40)) and $"Reset Timer".time_left == 0:
		
		$"Reset Timer".start()
		possible_stuck_pos = global_position

func _on_reset_timer_timeout():
	
	# If the vehicle has moved too much or too little from its last position it is reset
	var dist_to_stuck_position = global_position.distance_to(possible_stuck_pos)
	if (dist_to_stuck_position < 1) or (dist_to_stuck_position > 40):

		global_position = last_checkpoint_position
		global_rotation = last_checkpoint_rotation
		
		linear_velocity = Vector3.ZERO
		angular_velocity = Vector3.ZERO
		
		engine_force = 0
		steering = 0
		
		$NavigationAgent3D.target_position = checkpoint_positions[checkpoint_pos_index]
		
func start_speed_boost():
	
	is_getting_boost = true
	max_speed = 400.0
	
	$"Boost Particles".emitting = true
	$"Boost Duration".start()
	
func _on_boost_duration_timeout():
	
	$"Boost Particles".emitting = false
	
	is_getting_boost = false
	max_speed = 200.0


Finally, here is a screenshot of the car in the editor:

Thank you in advance if you decide to try to help me.

If your player looking backwards when you used look at then after look at, do that rotation_degrees.y -= 180, I have another one line code way but I forgot that, I need to open my computer therefore I have a code, im not sure it will work, you can just do that look_at(-next_pos)

1 Like

look_at has a third parameter “use_model_front” that you want to enable. Godot encouraged making models face -Z but most 3D programs use +Z, this parameter will flip them to look in the other direction. You will see a similar parameter in PathFollow3D.

look_at(next_pos, Vector3.UP, true)
1 Like

This worked. Thank you so much!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.