#get_position() returning position of node in it's own scene, not game scene (SOLVED!)

1 messages · Page 1 of 1 (latest)

sudden gust
#

I'm trying to make my enemies spawn, and then follow and look at the player- but for some reason get_position() is returning 0,0, i'm assuming it's accessing that from the player scene itself, but I'm not sure. I've been googling for hours and Im stumped, 'll reply with a video of the problem as well.

class_name enemy

var current_pos
@export var speed = 100
var screen_size
var player_pos

func _ready():
    player_pos = get_node(":/root/main/player").get_position()
    
    screen_size = get_viewport_rect().size

func _physics_process(float) -> void:
    current_pos = player_pos

    look_at(current_pos)
    velocity = (current_pos - position).normalized() * speed
    move_and_slide()

    position = position.clamp(Vector2.ZERO, screen_size) 
    
func _process(float) -> void:
    print(current_pos)```
polar spade
#

you need to use get_global_position() for that

#

or wait no

#

you're getting it in the ready function which only happens at the very beginning

sudden gust
#

should I move it to physics process?

polar spade
#

yes

sudden gust
#

LOLL if its that simple 😭

polar spade
#

also I'd recommend you add the player to a group so it's cleaner to get a reference to it

#

just get_tree().get_first_node_in_group('player')

sudden gust
#

so when I move ```player_pos = get_node(":/root/main/player").get_position()

sudden gust
polar spade
#

u don't have to do that but it makes the code cleaner

sudden gust
#

its good practice is what ur saying

polar spade
#

you can add an @onready variable at the beginning of the script for the player

#

typing code on mobile is painful 😭

sudden gust
#

thanks so much for your help. I had a syntax error as well so thats why moving it to physics process wasnt working but now its working!

#

i had a colon in the node path

#

get_position() returning position of node in it's own scene, not game scene (SOLVED!)

polar spade
#

ah

#

also why do you need the screen size

sudden gust
#

ah yeah so my camera is zoomed and the actual viewport is a bit bigger, so its just so the enemy and player cant go beyond

polar spade
#

does the camera move?

#

also it looks like with the way you amde it the player/enemy can only be in the top right quadrant of the screen, no?

sudden gust
#

no I dont believe so

polar spade
#

actually

#

wait no iut just doesn't work

#

i'm confused

sudden gust
sudden gust