I’m still a beginner in Godot, and i’m having troubles on one simple thing
I am trying to start a basic enemy AI where the enemy walks up to the player, using a NavAgent and everything. But, the code that i picked from a channel to help me, have this part of the code that, i have tried messing with it a little, and it seems to not exist in Godot 4.2 at least. And that is the target_position or the set_target_position.
Below is my code, if you all want anything more just ask me:
extends CharacterBody3D
var player = null
const SPEED = 6.0
@export var player_path : NodePath
@onready var nav_agent = $NavigationAgent3D
func _ready():
player = get_node(player_path)
func _process(delta):
velocity = Vector3.ZERO #the line right below is the problem
nav_agent.target_position(player.global_transform.origin)
var next_nav_point = nav_agent.get_next_path_position()
velocity = (next_nav_point - global_transform.origin).normalized() * SPEED
move_and_slide()
Change this line to @onready var nav_agent = $NavigationAgent3D as NavigationAgent3D
and target_position will come up in the autocomplete.
This line doesn’t make any sense: nav_agent.target_position(player.global_transform.origin)
target_position is a property. A property is a value that is either read or written.
I am guessing you want to change the property so change the line to this: nav_agent.target_position = player.global_transform.origin
Hi, i’ve made the changes that you told me to do. Here’s what i found
Putting now “nav_agent.target_position = player.global_transform.origin” gives this line of error => Invalid set index ‘target_position’ (on base: ‘null instance’) with value of type ‘Vector3’.
(i really don’t know what it means)
And removing the $ simbol from NavigationAgent3D, appears an error quoting this line of code “var next_nav_point = nav_agent.get_next_path_position()”, saying that it can’t call this function, and that i need to make an instance instead.
I am just picking this code from youtube, so maybe i just need to search anywhere better…
Ok, first off this is exactly how the line should look (I am not telling you to remove the $ I am telling you to add the ‘as’ clause:) @onready var nav_agent = $NavigationAgent3D as NavigationAgent3D
The error you are receiving Invalid set index ‘target_position’ (on base: ‘null instance’) with value of type ‘Vector3’
is telling you that the variable nav_agent isn’t set to anything (therefore ‘null instance’)
That means that this line @onready var nav_agent = $NavigationAgent3D
is not working. It is likely the path to the NavigationAgent3D node is invalid.
You will need to show your scene tree for anyone to figure out why.
However, it seems you are very new to coding and GODOT and could do with following a few starter tutorials before jumping out.
The $ symbol is shorthand for get_node(). So the $NaviationAgent3D is telling GODOT to get_node("NavigationAgent3D"). get_node() returns the base class Node type so despite your NavigationAgent3D being a NavigationAgent3D type node it gets returned as a base class Node and subsequently autocomplete doesn’t see the additional methods and properties of the NavigationAgent3D node.
(Under the hood the node returned is still a NavigationAgent3D and you can access all its members it just doesn’t autocomplete for you)
Adding the ‘as’ clause changes the returned value into a NavigationAgent3D type.
And autocomplete is a common term used to describe how the code editor suggests code completions for you.
Hey! I am really sorry for keeping you waiting, i was busy in this meantime.
So, I learned a little with what you said, and i could make some changes, some that were quite obvious but still lol
But, now i’m getting this message: “Invalid get index ‘global_transform’ (on base: ‘null instance’).”
Referencing this line here:
nav_agent.target_position = player.global_transform.origin
Oh, and just so you are aware, i didn’t made any other changes in my script, and that could affect the code. I mainly was experimenting other things and then decided to come back to deal with the enemy AI
Any error in “Invalid get index ____ (on base: ‘null instance’)” means variable to the left of your ___ is null, in this case your player.global_transform means it could not find the player, maybe the player_path wasn’t set?
@export var player_path : NodePath @onready var nav_agent = $NavigationAgent3D as NavigationAgent3D
I guess it really could be it, i followed the YT tutorial and it led me to this. What could i change then? This “NodePath” is just…there? even i found it strange
Now he actually iniciates running to the player…kinda, but then he just takes flight and goes outbounds? It’s really comical actually, but it’s just so absurd. What could be doing this?
He goes to where the player spawns (and not exactly, i will take about that in a minute), but then when he reaches the player, he just flies and goes on his way or whatever and goes forever, bugging on its way. As to why he doesn’t really get to the player, in the first time, i thought he was atleast trying to get to where the player is, but then i moved the player3D to another place and he just, followed the same path, ignoring the player’s new location.
I really don’t know why it’s so glitchy, here’s the Script of the enemy if anyone can get over it (maybe it’s the NavRegion or whatever?):
extends CharacterBody3D
var player = null
const SPEED = 1.0
@export var player_path : NodePath @onready var nav_agent = $NavigationAgent3D as NavigationAgent3D
func _ready():
player = get_node(player_path)
func _process(delta):
velocity = Vector3.ZERO #the line right below is the problem
nav_agent.target_position = player.global_transform.origin
var next_nav_point = nav_agent.get_next_path_position()
velocity = (next_nav_point - global_transform.origin).normalized() * SPEED
move_and_slide()
Sorry if my question might be dumb. But what is this exactly? Where can i find this “visible navigation”? Maybe this bug is too complex, i would need to create a new topic on this…
These options help termendously to debug collision and navigation problems, it aught to give you something better to lead on than “going the wrong way”