# Enemy follow player

**URL:** <https://forum.defold.com/t/enemy-follow-player/71930>\
**Category:** Questions\
**Created:** [October 23, 2022, 4:25pm UTC](https://forum.defold.com/t/enemy-follow-player/71930 "2022-10-23T16:25:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![OURABIG](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ourabig/32/29701_2.png) [@OURABIG](https://forum.defold.com/u/OURABIG)\
**Post date:** [October 23, 2022, 4:25pm UTC](https://forum.defold.com/t/enemy-follow-player/71930/1 "2022-10-23T16:25:09Z")

</div>

hello i want to make the enemy follow the player so please can someone help me I tried to give the enemy the player position using

> go.get\_position(“/player”)

but when I make

> go.set\_position(playerpos)  
> the enemy don’t follow the player it just like spawn in the player position every frame

---

<div class="post-metadata">

**Author:** ![Ragetto](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ragetto/32/32785_2.png) [@Ragetto](https://forum.defold.com/u/Ragetto)\
**Post date:** [October 23, 2022, 5:12pm UTC](https://forum.defold.com/t/enemy-follow-player/71930/2 "2022-10-23T17:12:41Z")

</div>

go.set\_position is used to (instantly) assign a position to a game object.

If you use it at every frame (with the player position as a parameter)… What you see is normal 🙂 the enemy teleports itself to the player’s position.

If you want your enemy to follow the player, you need to make it move toward this player, at a certain speed etc.

* * *

2 threads talking about that:

> [@Game Object Follow Mouse Cursor at Certain Speed](https://forum.defold.com/t/game-object-follow-mouse-cursor-at-certain-speed/69051/2):
>
> In this case you need two things: A target/destination position for your game object to move towards Update the target/destination position with the mouse position each time the mouse moves It would look something like this: function init(self) -- get focus msg.post(".", "acquire\_input\_focus") -- speed in pixels/second self.speed = 200 -- the target position to move towards self.target\_position = go.get\_position() end function update(self, dt) local current\_position = go.get\_posi…

> [@How to make a Game Object follow another Game object?](https://forum.defold.com/t/how-to-make-a-game-object-follow-another-game-object/67176/4):
>
> There are still many ways to have something follow another thing with the following depend on how you want it to look. Here’s a stand alone example of just following based on max distance allowed. Also includes z sorting. [BoxFollow.zip](https://defold-forum-bucket.nbg1.your-objectstorage.com/original/3X/8/2/820aa66d604ca7429febc90967d028698ea9458f.zip) (6.4 KB) [follow] The code for the box to follow the hero object. local hero\_go = "/char\_hero" local max\_distance = 20 local speed = 100 local function update\_sorting() local position = go.get\_position() position.z = position.y \* -0.0001 go.set\_position(po…

* * *

Also, maybe you could (re)take a look at the war battles tutorial and try to make the missile aim for a specific object (instead of just moving straight). Would be easier to understand how vectors etc. work.

---

<div class="post-metadata">

**Author:** ![OURABIG](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ourabig/32/29701_2.png) [@OURABIG](https://forum.defold.com/u/OURABIG)\
**Post date:** [October 23, 2022, 5:16pm UTC](https://forum.defold.com/t/enemy-follow-player/71930/3 "2022-10-23T17:16:45Z")

</div>

thanks
