Material Box

Material Box

WEB Design & Material Images

Godot - Get direction to target node

Godot

This is how to get the direction to a target node using GDScript in Godot 4.


You can get the direction to a target node using direction_to().

direction_to() has the following characteristics:
- Specify the position of the target node as an argument
- position, global_position can be specified
- Returns a vector to the target node

var direction = global_position.direction_to(target.global_position)
# (0.999718, 0.023731)

To move towards the target node, multiply the direction by speed to update the velocity property, and execute the move_and_slide() function

velocity = global_position.direction_to(target.global_position) * speed
move_and_slide()

Godot

TitleGodot - Get direction to target node

CategoryGodot

Created

Update

AuthorYousuke.U