Giving player position to enemy

The enemies in my game need to know the player’s position at all times. How do I give the player’s position to enemies?

You can just call go.get_position(player_id) in enemy script.

function init(self)
	self.velocity = vmath.vector3()
end

function update(self, dt)
	local playerPos = go.get_position(player)
	print (playerPos)
	self.velocity = 2 * (playerPos - go.get_position())
   	go.set_position(go.get_position() + self.velocity)
end

For some reason, the print function is printing the position of the enemy, not the player.

1 Like

Where do you set the value of player? It needs to be the correct URL of the player game object. If it’s nil then go.get_position will give you the position of the current game object (the enemy).

3 Likes
local playerPos = go.get_position("/player")

:slight_smile:

If you want to find “player” out of the collection, it has to be hashed as given, unless you had already set player = to the “player” object.

Oh @ross.grams, you took the inside lane again :laughing:.

3 Likes