teleport a unit back on it own path.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By quizzcode

hello there !
Working on the “power” of one of my towers for my tower defense game.
One of them would be to teleport back a unit along his path.
The path isn’t pre traced as the towers you place make the maze.
So im using get_simple_path.

Do see any way to “track back” a certain amount of pixel ?
Im thinking of doing a get_simple_path from mob.position to start_spawn —> but how do i define a point X pixel from starting point ? is that feasible with get_simple_path ?

:bust_in_silhouette: Reply From: Magso

get_simple_path returns an array of points, these can be added to a curve on a path node using a for loop and add_point. A child PathFollow node can then work with pixel units along that path.

thank you for your answer.
But wouldn’t that make my mob walk to the point rather than reposition him there ?

quizzcode | 2020-05-20 07:52

Also, since the points are automatically generated.
It would be a far point. I’d like to be precise on how many PX back i can Teleport the unit.

quizzcode | 2020-05-20 07:59

The offset of PathFollow2D is basically the exact distance along the path in pixel units, the only way your mob would walk there is if you are using lerp to change a separate value for the offset.

Magso | 2020-05-20 08:27

that’s awesome ! Thx i didn’t know.
I’m going to look into how to formulate that now.

quizzcode | 2020-05-20 08:29

Hey Magso !
do you have any example of i could learn from ?

these can be added to a curve on a path node using a for loop and add_point.

quizzcode | 2020-05-24 19:23

Here’s a simple example of using get_simple_path with the path and pathfollow nodes.

func _ready():
	var path = $Navigation.get_simple_path($StartPoint.translation, $EndPoint.translation)
	for i in path.size():
		$Path.curve.add_point(path[i])

func _process(delta):
	$Path/PathFollow.offset += delta*10

Edit I forgot you’re working in 2D luckily it’s not too different.

func _ready():
	var path = $Navigation2D.get_simple_path($StartPoint.position, $EndPoint.position)
	for i in path.size():
		$Path2D.curve.add_point(path[i])

func _process(delta):
	$Path2D/PathFollow2D.offset += delta*10

Magso | 2020-05-24 20:24

sweet ! thanks Magso.
so, if I got this right.

set my path backward (done)
which returns an array of points.
which i can append a new point to a specific pixel distance.

Im reading the doc that you link but I can’t seems to find how to “mesure” the distance in px from on that path.
Adding a point seems fairly easy, i will need to figure out how to get the position.x/.x from it to then “teleport” my unit on it.

EDIT :
here is a quick graph to make sure we’re on the same page :smiley:

just realized that the link was for 3d. i read the 2d and I found this :

Vector2 interpolate_baked ( float offset, bool cubic=false ) const
Returns a point within the curve at position offset, where offset is measured as a pixel distance along the curve.

To do that, it finds the two cached points where the offset lies between, then interpolates the values

quizzcode | 2020-05-24 21:41