0 votes

(these 2 lines is not apart of the code) there are underscores in the code but for some reason, they don't show.
extends KinematicBody2D

const GRAVITY = 15
const ACCELARATION = 50
const MAX_SPEED = 200

var enemy = self
var speed = 15
onready var target = getparent().getnode("player")

func ready():
set
physics_process(true)
pass

func fixedprocess(delta):
var direction = (target.getglobaltransform().x - enemy.getglobaltransform()).normalized()
moveandcollide(direction*speed)
pass

asked Jul 3, 2018 in Engine by Zubayer (16 points)

When you're posting code here, either put four spaces in front of each line, or use the code formatting button, which looks like {}

When you're posting code here, either put four spaces in front of each line, or use the code formatting button, which looks like {}

Also, if you're using 3.0, you don't need set_physics_process(true). And those pass statements are meaningless and don't do anything at all.

1 Answer

0 votes

Try this:

var direction = (target.global_position - global_position).normalized()

Note that the self reference is unnecessary. The script is running on the enemy, so it has access to the running node's properties directly.

answered Jul 3, 2018 by kidscancode (10,928 points)

this is the code I wrote and it didn't work
extends KinematicBody2D

const GRAVITY = 15
const ACCELARATION = 50

var speed = 15
onready var target = getparent().getnode("player")

func fixedprocess(delta):
var direction = (target.globalposition - globalposition).normalized()
moveandcollide(direction*speed)

How did it not work? Did you get an error message?

There's a good example of follow movement (following the mouse) in the 2D movement tutorial in the docs.

It didnt show an error message the enemy just didnt follow the player

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.