my enemy wont follow the player.....

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

(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 = get_parent().get_node(“player”)

func _ready():
set_physics_process(true)
pass

func _fixed_process(delta):
var direction = (target.get_global_transform().x - enemy.get_global_transform()).normalized()
move_and_collide(direction*speed)
pass

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

kidscancode | 2018-07-03 20:48

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.

kidscancode | 2018-07-03 20:50

:bust_in_silhouette: Reply From: kidscancode

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.

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 = get_parent().get_node(“player”)

func _fixed_process(delta):
var direction = (target.global_position - global_position).normalized()
move_and_collide(direction*speed)

Zubayer | 2018-07-03 21:00

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.

kidscancode | 2018-07-03 21:11

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

Zubayer | 2018-07-04 06:51