0 votes

My code worked perfectly and something happened, now, I don't know why, the player node returns null and all stop working.

extends KinematicBody2D

var motion = Vector2()

func _ready():
   pass

func _physics_process(delta):
  var player = get_parent().get_node("player")

  position += (player.position - position)/50

  look_at(player.position)

motion = move_and_collide(motion)
in Engine by (12 points)

First of all calling var player = get_parent().get_node("player") in every physics frame is wrong. It's enough to do this once in _ready().

Second, make sure your composed tree hierarchy is matching your query, use print_tree_pretty()to see.

1 Answer

0 votes

Try this:

extends KinematicBody2D

var motion = Vector2()

var player = get_node(drag the player node inbetween these parentheses)

func _physics_process(delta):

  motion += (player.position - position)/50

  look_at(player.position)

motion = move_and_collide(motion)

Don't make a variable that is constant be called every frame, also remember to have the correct path to your player node.

by (19 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.