Car ia with vehiclebody3D, for a vehicle combat game

Godot Version

v.4.3.Stable.Official(Mobile)

Question

Hi, I need help in a godot project, I need to know how to create a small AI to make a vehiclebody3D, which is the enemy, collide and follow the player, which is also a vehiclebody3D, and I can’t find tutorials to help me, can anyone pass me links or tell me what I can do?

Something I forgot to mention, is that I also want the player to be able to collide with the enemy. In short, a car combat game, if someone could help me, I would be very grateful.``

Well, a VehicleBody3D should already be capable of colliding with another physics object so… collision done. If you have issues with this, please provide more detailed information about your project.

To create an AI that is capable of following another object (e.g. your player), you have to first define what following means in terms of data.

One way of defining following is: to move towards the target. Using available data, this means that your enemy should be looking towards your player, and be moving closer to them. To define it more explicitly:

Target enemy state

  • The forward vector (Z-basis vector) should align with the vector pointing to the target.
  • The velocity vector should align with the vector pointing to the target.

The forward vector and velocity vector may be nearly identical, but it is important to distinguish the two in case you intend for the enemy’s vehicle configuration to allow sliding/drifting.

With the target state defined, you can now start designing and implementing the behaviour that attempts to achieve this state. You can start off with steering the vehicle in the correct direction by turning the wheels and accelerating the car.


There are other things to consider, such as collision avoidance, but this should get you started. Let me know if you have any further questions.

Here’s the short version of how to do that in Godot (4.x):

  1. Use Navigation for chasing:

    • Add a NavigationRegion3D and bake a navmesh of your track or terrain.
    • Give your enemy car a NavigationAgent3D.
    • Every frame, set the agent’s target_position to the player’s global position.
    • Get the next_path_position and steer your VehicleBody3D toward it.
    # EnemyCar.gd
    extends VehicleBody3D
    
    @onready var agent = $NavigationAgent3D
    @export var speed = 10.0
    
    func _physics_process(delta):
        agent.target_position = get_node("/root/Player").global_position
        var direction = (agent.get_next_path_position() - global_position).normalized()
        steering = direction.x * 0.5  # tweak this
        engine_force = speed
    
  2. Enable collision between cars:

    • Both the player and enemy should use VehicleBody3D (RigidBody-type).
    • Give each one a CollisionShape3D (Box, Cylinder, etc.).
    • They’ll naturally collide under physics if on the same collision layer and mask.
  3. Tuning:

    • Adjust steering, engine_force, and damping for realistic pursuit.
    • Optionally add a distance check so the enemy stops or attacks when close.

That’s the basic setup — the enemy will chase and collide with the player.
If you want, I can share a minimal working example script/project layout. Would you like that?

1 Like