The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

So in my game I need the enemy to look at the player and follow it, but because they are separate scenes, I don't know how to refer to the player from the enemy script.

(less important)
v v v v
In addition to this, how can I make the enemy always look at the player (do I use the "look_at" method or something) and also how do I make it move constantly forwards towards the player

P.S. game is in 2D

Godot version 3.4
in Engine by (14 points)
edited by

1 Answer

0 votes

enemy script:
var player = getparent().getnode("player") = find player .

lookat(player.globaltransform.origin, Vector3.UP)

by (785 points)

I forgot to mention, my game is 2D, so how would that go

var player = getparent().getnode("player") = find player

it says player is not define in the current scope, I tried changing the name to the scene name and it still didn't work
this is my structure btw:

Player <------- this Node is what i want the enemy to look at
Ring <------ is just kind the background
Enemy <------ tryna write script for this node

btw the Player, Ring and the Enemy nodes are scenes that i have instanced into the main scene

--- Enemy (script is here)

var player = getparent().getnode("Player" , "Camera2D", "Ring")


--- Enemy
--------Another Node(script is here). (child Enemy)

var player = getparent().getparent().get_node("Player" , "Camera2D", "Ring")

just to clarify the structure goes like this (my bad I didn't realise the spaces didn't show up)


also because the enemy is like a separate scene, the player does not exist with it because when I code for the Enemy, it is assuming the structure of the enemy scene which look like


the enemy exists separately as it is in a different, same with the player scene, it is just that I instanced both the player scene and the enemy scene to the main scene, I am pretty sure that is why it isn't working. I'm very new to GDscript btw

Thanks man, I'm sure it will help me, but when I said new, I meant I know a bit more than a beginner, but not enough to make a full game solo

sorry friend. I think maybe I didn't understand the problem clearly.

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.