Okay, so I had code for a chase state for my enemy AI and it looked something like this:
raycast.rotation_degrees.y = 0
if entered_area == false:
focus_on(target,10)
if raycast.get_collider() == target:
move_foward(5)
anim_player.play("run")
else:
anim_player.play("idle")
if transform.origin.distance_to(target.transform.origin) > 50:
state = PATROL
elif entered_area == true:
state = ATTACK
So what the chase state does it it makes the enemy chase down the player when it can directly see the player but, the enemy will stay still if the player isn't in sight. The enemy will also go back to the patrol state if the player moves far away. So what's the problem here? Well, the chasing state is incredibly simplistic; it would make more sense if the enemy could chase the player even if they aren't directly looking at them. So I followed a tutorial on 3d pathfinding by Youtuber Garbaj. Here is it for reference:
Now I've ended with pathfinding code that looks like this:
#func _on_Timer2_timeout():
# move_to(target.global_transform.origin)
#func path_finding_code():
# if path_node < path.size():
# var dir_to_path = (path[path_node] - global_transform.origin)
# if dir_to_path.length() < 1:
# path_node += 1
# else:
# move_and_slide(dir_to_path.normalized() * speed, Vector3.UP)
#func move_to(target_pos):
# path = nav.get_simple_path(global_transform.origin, target_pos)
# path_node = 0
Now the reason why I commented out all the code is, despite the fact that it works, I don't know how to seamlessly integrate into my already existing chase state. Another problem with my 3d pathfinding code is that, even though Garbaj's tutorial is easy to follow, I don't fully understand the 3d pathfinding code and I can't make the desired improvements to it. For example, the enemy ai doesn't constantly chase the player without stopping. Instead, it pauses sometime due to the timer and I don't even know how to fix that. I know that they're other tutorials out their with slightly better code for 3d pathfinding but, I'm sticking to Garbaj's tutorials because they're short and easy to follow. A tutorial that I personally think has better 3d pathfinding code than that of Garbaj's tutorial on the subject is one by Youtuber rayuse rp, as his end result seems more promising. However, as I've stated, it's just way easier to follower Garbaj's tutorial on the subject. Here's rayuse rp's tutorial for reference:
So can anybody help me out with this problem? Even though I'm not a professional programmer, I've been in situations where I've ended up finding my own creative coding solutions to certain problems. For example, after days of asking people for help on how to the weapon switching system for my game feel more the ones in old-school first person shooters( else where), I managed to find the solution on my own( which I might share when/if I manage to finish the game). However, I'm here working with code that I don't fully understand and it does suck the motivation out of me. Garbaj does a good job of explaining the code in his video and I do feel that I could understand it if, I were to repeatedly follow his tutorial over and over again; I also find myself feeling like I understand the code the more I read it back to myself. However, I want to get my game finished in a timely fashion and I've already got the code "working" anyway; I just need to know where to put it and how to improve it. So is there anybody out there that can help me?
On a side note, there are some questions about 3d pathfinding that I've been dying to ask for a while and I'll ask them here. How creative can a person get with 3d pathfinding in Godot? Are there some ways to do 3d pathfinding in Godot that are arguably objectively better than other ways of doing 3d pathfinding in Godot( with the navigation mesh)? I'm asking these questions because I've followed multiple tutorials on this subject and, even though some were more difficult to follow than others, the solutions that they came up with seem incredibly similar to each other where coding is concerned. This seems to imply that there is only one or a few ways to do 3d pathfinding properly in Godot with a navigation mesh; is this the case? Answers to these questions might influence how I address issues regarding 3d pathfinding in the future.