Trying to find a solution for my enemy navigation
Asked Answered
J

2

0

Hey!
I implemented a basic chasing state for my enemy in my 2D Top-Down game. The issue I'm having is since the origin of all my characters are on the feet (for more consistent Y sorting), sometimes my enemy get stuck on corners since it's collision is above his origin point.
Here is a video showing what's happening:

And here is my code:

func fixed_update(delta : float):
	
	controller.move_dir = get_direction_to_chase_target(col if col != null else actor_to_chase)
	controller.update_velocity(controller.speed_walk,delta)
	controller.move_and_slide()

func get_direction_to_chase_target(target_actor) -> Vector2:
	if target_actor == null:
		return Vector2.ZERO
	if nav_agent == null:
		return Vector2.ZERO
	nav_agent.target_position = target_actor.global_position
	var current_position = controller.global_position
	#Get Next position from Nav Agent Calculations
	var next_position = nav_agent.get_next_path_position()
	return current_position.direction_to(next_position)

So, how I can I make a proper avoidance system when this happens? I'm thinking about raycasting when this happens but not sure how to implement the actual avoidance.
Increasing the navmesh radius won't work since it would need a really big radius to avoid this problem completely and it'll probably cause issues on tight corridors
Thanks!

Jenelljenelle answered 29/1 at 17:20 Comment(0)
M
0

Jenelljenelle Can you make the corners slightly angled or round?

Mika answered 30/1 at 12:42 Comment(0)
M
0

Jenelljenelle You can increase the agent's radius through the NavigationPolygon resource in the NavigationRegion2D. It will be under "Agents -> Radius" The default is 10px. Just increase it until they don't get stuck on corners.

Maidamaidan answered 1/2 at 22:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.