Topic was automatically imported from the old Question2Answer platform.
Asked By
Master0987
Im trying to make the enemy follow the player. but every name i get a different error code
my scene hiarchy
Node2d
player(characterbody2d)
child: camera2d
Tilemap
navigation region 2d
enemy(characterbody2d)
child:NavigationAgent2d
my enemy code is
@export var target_location_path:NodePath
@onready var target_location:Node2D = get_node("player")
@onready var nav_agent = $NavigationAgent2D2
func _ready():
nav_agent.set_target_position(target_location.global_position)
func _physics_process(delta):
if nav_agent.is_target_reached():
var next_location = nav_agent.get_next_position()
var direction = global_position.direction_to(next_location)
global_position += direction * delta
func _on_area_2d_body_entered(body):
if body.is_in_group("bullet"):
queue_free()
any and all help is appreciated
error codes:
invalid get index ‘global_transform’ (on base ‘PackedScene’)
invalid get index ‘global_transform’ (on base ‘null instance’)
Please format your code using the code sample option when you write your question. That makes it much easier to read for others and to help you. Also I don’t think the moderators like it if you post basically the same question multiple times. Maybe read the rules for the QA section first before you write… Rules for QA
However the code you posted is a total mess missing several underscores in the names of functions and so on. The ‘globaltransform’ in your error message is invalid because it should be global_transform for example.
Edit: The last paragraph is now wrong. Thanks to Jgodfrey for formatting so we can look at the real problems.
Marco- | 2023-03-16 21:27
Edited to fix code formatting, but please do that yourself in future posts. To do that, paste code into your message, select it (just the code), and then press the {} button to format it for the forum.
And, do pay attention to the friendly “don’t spam the board with the same message” comments you’ve received here and elsewhere.
You export the variable target_location_path to the editor without using it. If you don’t need it delte it so it can’t confuse anybody.
You load the nav_agent with NavigationAgent2D2 is the 2 at the end correct? If not remove it so you load your variables correct.
You call the loaded Player node target_position which is semantically confusing. Better call it what it is (for example player or player_node) or extract the relevant information directly with @onready var target_location: Vector2 = get_node("player").global_position
NavigationAgent2D does not have the method set_target_position. There is the property target_position that you can set directly using nav_agent.target_position = target_location. Note that you have to apply the third step for this to work this way.
In _physics_process you define var next_path_position: Vector2 = nav_agent.get_next_path_position()
Then let your enemy look towards the direction it should go look_at(next_path_position)
Calculate the direction towards the new_path_position by subtracting your enemy position from it and normalizing it var direction: Vector2 = (new_path_position - position).normalized()
Multiply the result with your defined speed and delta and store that in the velocity property that you got by inheriting from CharacterBody2D: velocity = direction * my_speed * delta
Use move_and_slide() to apply the movement to your enemy.
Note that your way of calculating direction would have meant that the character would ignore its path and try to run to your player in a straight line. If there would be a wall that NPC would never reach its target.
The code should now look like this:
@export var my_speed: float = 200.0
@onready var target_location: Vector2 = get_node("player").global_position
@onready var nav_agent: NavigationAgent2D = $NavigationAgent2D2
func _ready() -> void:
nav_agent.target_position = target_location
func _physics_process(delta) -> void:
var next_path_position: Vector2 = nav_agent.get_next_path_position()
look_at(next_path_position)
var direction: Vector2 = (new_path_position - position).normalized()
velocity = direction * my_speed * delta
move_and_slide()
This should work.
And please remember this QA Section is great but sometimes the same question might already have been asked here or somewhere else. And also please don’t post the same question over and over again with slight deviations because that might annoy other people or cause other question to go unnoticed.
And last but not least please don’t call it ‘G a dot’ it is ‘G o dot’ with two 'o’s