I'm currently working on my ai and am needing some help as I'm converting my game from Game Maker to Godot and so far it's been pretty easy and GDScript's pretty easy to grasp but I have a few questions.<br><br>How do you check the distance from Node A to Node B?<br>In game maker there's a distance_to_object or point_to_distance(which I use) and a point_direction function.<br><br>Now I have tried and created a distance to function.<br>

func distance_to(n1,n2):<br>#Player node<br><br>n1=get_node("Player")<br><br>#Enemy or NPC node<br><br>n2=get_node("Enemy")<br><br>#get how far n1 is from n2 or vice versa<br><br>var xdist=n1.x - n2.x <br>var ydist=n1.y - n2.y<br>return (xdist xdist) + (ydist ydist)

For point_direction "gml, kinda long and would take awhile to edit to gdscript" and also a vector based direction function "I'd write it as a function".<br><br>Vector version: direction=rad2deg(atan2(vec1.y - vec2.y, vec2.x - vec1.x))<br><br>GML "to be changed later?"<br><br>x1=argument0; <br><br><br>

You need to catch the positions. I assume your objects inherits from 2DNode.

[code] func checkDist(node1, node2): var a=Vector2( node1.get_pos() - node2.get_pos() ) return sqrt( (a.x a.x) + (a.y a.y) )

You can call this function like this: checkDist( get_node(&quot;Player&quot;), get_node(&quot;Enemy&quot;) )

maybe it could be faster (for the computer) to get the nodes on &quot;_ready&quot; time if your function will be called many times.

onready var player=get_node(&quot;Player&quot;) onready var enemy=get_node(&quot;Enemy&quot;)

#and the call

checkDist(player, enemy) [/code]

If you want the distance from the current node :

[code]

func distanceTo(targetNode): var a=Vector2( targetNode.get_pos() - get_pos() ) return sqrt( (a.x a.x) + (a.y a.y) )

[/code]

I haven&#039;t tested that but the syntax seems correct to me.

7 years later