Godot Version
4.2
Question
how would i define a specific point for an object to move to and back
Explain what you want
i want the one i circled to run up to whichever one it chooses (i prob wont need help with rng) and attack, and then run back
mario and luigi style lol
Youâll need to define a position by each enemy, then use a tween to put the player at that location. Adding a Marker2D child for instance
func move_to_enemy(enemy: Node2D) -> void:
var end_point: Vector2 = enemy.get_node("Marker2D").global_position
var tween := create_tween().set_trans(Tween.TRANS_QUAD)
tween.tween_property(self, "global_position", end_point)
moving them back will be a similar function, using a variable to keep track of their original position instead of end_point
i tried making a script and adding this code but it says the last line needs four arguments
Sorry, the last argument it wants is duration in seconds, how long it takes to complete the tween.
ah mb, lemme test it
when i call the move to enemy function what would i put as the argument it needs?
The enemy to move towards. This enemy will have to have a Marker2D child for positioning in the example, did you change that line or are you prepared for that?
It would move to the random target, if it can find them. Maybe you need to add your playerâs to a group so it can select via
var player_target = get_tree().get_nodes_in_group("Players").pick_random()
move_to_enemy(player_target)
Then your players need the same Marker2D name
okey lemme try it
Everything is wrong there. move_to_enemy must be handled every frame. it has to be alive. for that it must be in the process(delta). get_node must be âSpotPH/spotterâ. it must start from the top of the tree to the targeted node. and âglobal_positionâ will not be found in the code. you must write a separate code to fix this position to. like a coordinate or another marker.
you can also find a way to record the current position of the sprite and use it later.
Canât use get_tree()
before a node is ready. My example was for the ready function. Make sure your players are in a group named âtargetsâ
func _ready() -> void:
var player_target = get_tree().get_nodes_in_group("Players").pick_random()
move_to_enemy(player_target)
enemy: Node2D
will be âSpotPHâ or âKyranPHâ so searching itâs children for âspotterâ (or now âtargetsâ) is correctglobal_position
is a valid Node2D propertyoh wait shit the error is âinvalid get index âglobal_positionâ (on base : ânull instanceâ)â
Cool, you put the group on the markers themselves. This isnât a bad idea, in fact probably better than what I recommended. The problem now is itâs trying to get a child that neither have (hence ânull instanceâ). You can remove the get_node("Marker2D")
for a shorter enemy.global_position