How to Implement AI for a Floating Boat in Unity?

I’m working on a 3D game where the player controls a boat and shoots at enemy boats.

Both the player and enemy boats:

  • Can move freely (rotate left/right, move forward and backward).
  • Can shoot from the left or right.
  • Require some time to reload after firing.
  • Have a FloatingRigidbody component, which implements buoyancy using Rigidbody.

My Goal

I want to implement a simple AI for the enemy boat that:

  • Follows the player.
  • Circles around the player when close.
  • Shoots when the player is in range and the cannon is loaded.

My Question

What would be the best approach to implementing this kind of boat AI?

What I’ve Tried

I initially used Unity’s AI navigation by attaching a NavMeshAgent to the enemy boat. However, this didn’t work as expected:

  • The boat moved as if the water was a flat surface, ignoring buoyancy.
  • It didn’t behave like a vehicle—it moved directly toward the player before rotating, which felt unnatural.

I haven’t worked with other AI techniques in Unity yet. I’ve come across ML Agents, but they seem like overkill for my needs.

I’d really appreciate any guidance on the best approach and any learning resources to explore.

You could simply add this as a layer atop the navmesh agent (well, under, as this would be done via child game objects). Basically, you let you nav mesh agent move around on a flat plane, while your boat, as a child object, moves up and down with the level of the water. You’ll probably want to do this regardless of AI solution.

Though I don’t know if some fancy AI navigation is needed here. Assuming these boats just want to come alongside the player to shoot them with broadside cannons, it’s kind of like a missile, only aiming for a position beside the target rather than the target itself.

You work out a position beside the player, and it continues to move forwards, turning at a certain speed, trying to maintain a direction that points itself towards the target.

Ok, so if I understand correctly, a simple script to control the enemy boat should be good enough?
I forgot to add that there may be some small obstacles in the game scene - maybe a couple of floating barrels here and there, nothing that would require A star search. It would be great for the enemy to avoid those obstacles, but nothing will happen if the enemy occasionally hits an obstacle. Also, the player can “hide” behind an obstacle - it’s not possible to shoot through obstacles.

It’ll be a good, simple starting point, that’s for sure. And then you have a basis from which to improve.

Knowing a boats velocity and its turning speed, you can roughly plot its projected heading over time. This can be used to make a series of points, with which can be used to query whether anything is in the way.

Then you can manipulate the plotted path based on obstacles to plot a path around them, and have the ship follow this path. Which I realise is starting to sound more like a proper AI navigation system.

This is just raycasts or other queries from the enemy to the player to determine whether anything is in the way.

1 Like

Ok thank you, that gives me a starting point.
I think I will stick with that “missle” approach as you mentioned and maybe add something on top of that to avoid obstacles later.
I also found a cool video describing how to make a very simple vehicle AI using existing vehicle controller, so I am going to leave it here: https://www.youtube.com/watch?v=xuCtxIcfboM&t=1s

1 Like