how work move_child?

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

Hello I’m making a simple game, I wrote the code, now I have to use move to get the car moving (I saw some tutorials)
But move is not just move_child, move_toward, do you know which one to use and what should I pass on to it?
Please comment

:bust_in_silhouette: Reply From: Sween123

Hi. There are many different ways to move (or animate) a node.
Which function to use depends on which type of node you want to move and how you want to move it.
Anyway, there is a general simple direct way to move any node, and that is by changing the position property of the node directly, which is quite helpful in many situations.

Example:

onready var myCar = get_node("The Car")
func teleport():
  myCar.position.x = 100
  myCar.position.y = 200
  #Above directly changes the position of myCar node to the coord (100, 200)
func _process(delta):
  myCar.position.x += 1
  #Slowly moves the car towards the right side

If you don’t use provided functions to move like here changing the position property, a function of your own is recommened. (For future convenience) Example:

func move(node, target_x, target_y):
  node.position.x = target_x
  node.position.y = target_y

To move (or more like animate) an object, like making a car slowly moving towards a point, nodes like AnimationPlayer or Tween may be helpful.

To move and having collisions, like moving your main character, nodes like KinematicBody may be helful (With functions like move_and_slide())

Thank you :wink:
Good luck

abbos | 2020-07-01 07:50