0 votes

after writing code I got "invalid get index ‘position’ (on base: ‘viewport’)." this error
code -
extends KinematicBody2D

var motion = Vector2()
func _ready():
pass # Replace with function body.

func physicsprocess(delta):
var Player = getparent().getnode(".")

position += (Player.position - position)/50
look_at(Player.position)

move_and_collide(motion)
Godot version 3.2.3
in Engine by (12 points)

1 Answer

0 votes

That error is because you're trying to move the root viewport. When you write:

var Player = get_parent().get_node(".")

You're getting the parent node - and then get_node(".") does nothing but still get the parent node. "." means yourself.

It seems this script is looking for the Player node. You haven't shown your node setup, so I can't tell you the correct path, but that is what you need to use.

For example, if the player is a sibling of this one, you might use

var Player = get_parent().get_node("Player")

which is the same as

var Player = get_node("../Player")
by (22,067 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 Frequently asked questions and 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 [email protected] with your username.