Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a Navigation Component for 2D and 3D #2

Open
AlecSouthward opened this issue Mar 10, 2024 · 0 comments
Open

Create a Navigation Component for 2D and 3D #2

AlecSouthward opened this issue Mar 10, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@AlecSouthward
Copy link
Owner

Could be a useful addition to the list of Components...

Previously written code for 2D version:

@icon("../../module.svg")
extends NavigationAgent2D
class_name NavigationComponent2D
## Component for simplifying the use of the NavigationAgent2D.
##
## @experimental

@export var velocity_component : VelocityComponent2D

## Minimum distance from the target position.
@export var min_target_dist := 3.0

## Speed at which the agent must slowdown when reaching the target.
@export var slowdown_rate := 1.0

## Target node to follow (Follows global position).
@export var target : Node2D

func _ready() -> void:
	if (!velocity_component):
		printerr("Missing velocity component on " + name + " in " + owner.name)

func _process(_delta: float) -> void:
	if (target):
		target_position = target.global_position

func get_target_dir() -> Vector2:
	var dir = (target_position - owner.global_position).normalized()
	return dir

func get_next_pos_dir() -> Vector2:
	var dir = (get_next_path_position() - owner.global_position).normalized()
	return dir

func follow_path():
	if (distance_to_target() < min_target_dist):
		velocity_component.slowdown(slowdown_rate)
		print("wo")
		return
	
	velocity_component.max_velocity(get_next_pos_dir())
	set_velocity_forced(velocity_component.velocity)
	print(velocity)
@AlecSouthward AlecSouthward added the enhancement New feature or request label Mar 10, 2024
@AlecSouthward AlecSouthward self-assigned this Mar 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant