body not following player

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

trying to add a character like the light that follows link in zelda

current script
extends KinematicBody

export var moveSpeed = 13

onready var me : Node = get_node(“/root/MainScene/Player/Stay_pos”)

func _physics_process(delta):

var dir = (me.translation - translation).normalized()

dir.y = 0

 # move the enemy towards the player
move_and_slide(dir * moveSpeed, Vector3.UP)

its a simple follow player script.it works fine but it refuses to stay on the position node above the players head for some reason unless the node is colliding with the floor. i tried various other nodes like a spatial, even attaching kinematicbody to its fellow kinematicbody to see if it would work but it started glitching.

so far this has been troubling as it is one of the core mechanics in my project. would appreciate it if someone told me what i’m doing wrong

:bust_in_silhouette: Reply From: drorya

I tried to use your code, and tweak it. This is what worked:

export var moveSpeed = 13

func _physics_process(delta):
var dir = (-self.translation - get_parent().translation).normalized()
dir.y = 0
# move the enemy towards the player
move_and_slide(dir * moveSpeed, Vector3.UP)

I just used “self” instead of “me”, and added a - sign before self.translation (because it was moving to the opposite direction)
tell me if it works for you :slight_smile:

Tried it but the body refused to move at all

Ogeeice | 2020-06-03 12:50