0 votes

Hey Everyone!

I am currently working on a Dungeon Crawler, where I want some flying bat enemy to follow the player. I am using a NavigationRegion3D Node with a baked Navigation Mesh and my GridMap node for the world is attached to that node.

Everything is working fine, but the enemy is staying very low on the floor. First I thought this is because the reference point of my player is at the bottom of the player model, but I have now added a Marker3D node in my player scene (at camera height) and set that as target.

I feel like the enemy is bound to the mesh that is rather low on the floor. Ideally i would like the enemy to randomly choose a height to fly at (within a certain range), but for now it would be fine to just offset the height of the enemy by a fixed amount.

This is my code to send the bat towards the player:

func _physics_process(delta):

var player_position = player.global_transform.origin
get_tree().call_group("enemies", "update_target_location",player_position)


The physicsprocess and updatetargetlocation function in the enemy looks like this:

func _physics_process(delta):
var current_location = global_transform.origin
var next_location = nav_agent.get_next_location()
var new_velocity = (next_location - current_location).normalized() * SPEED

velocity = new_velocity

var player_position = player.global_transform.origin
self.rotate_object_local(Vector3(0,1,0), 3.14)

func update_target_location(target_location):

Variables here are:

@onready var nav_agent = $NavigationAgent3D
@onready var anim_player = $AnimationPlayer
@onready var player = $"../PlayerCharacter"
var SPEED = 3.0

Please let me know if you need further details. Thank you very much in advance!


Godot version v4.0.beta10.official [d0398f62f]
in Engine by (18 points)

1 Answer

0 votes

Nothing is binding you to the navigation mesh surface, you are the puppet master. If you want a "flying" unit to have height variation on the y-axis do so in your movement code and add / reduce the height with an offset. Calculate your next position first as if the unit is on ground and then add your offset to the y-axis of the visual or physics representation of the unit.

This "flying" behavior is completely unrelated to the actual pathfinding system. Apart from very technically specific games that use voxel navigation a flying unit in most games e.g. RTS games, is just a normal "ground" unit where the pathfinding still happens at ground level with a mostly flat navigation mesh and the visual or physics representation of the unit is then offset on the y-axis.

by (102 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 Frequently asked questions and 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 [email protected] with your username.