# Spawning one enemy near the player at random times

**URL:** <https://forums.unrealengine.com/t/spawning-one-enemy-near-the-player-at-random-times/483750>\
**Category:** Community & Industry Discussion\
**Tags:** horror, ai-move-to, spawning, unreal-engine, enemy, question\
**Created:** [May 22, 2021, 12:17pm UTC](https://forums.unrealengine.com/t/spawning-one-enemy-near-the-player-at-random-times/483750 "2021-05-22T12:17:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Catleopatra](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Catleopatra](https://forums.unrealengine.com/u/Catleopatra)\
**Post date:** [May 22, 2021, 12:17pm UTC](https://forums.unrealengine.com/t/spawning-one-enemy-near-the-player-at-random-times/483750/1 "2021-05-22T12:17:20Z")

</div>

Hello, I have been working on a horror game for my college project and I only have m AI left. I have created an Ai that can patrol the environment (a house) and can catch the player. However, I want the AI to spawn at random times near the player (for example behind the player) instead of having the AI patrolling the house all the time.

I have watched tutorials on spawning enemy AI but they are about spawning multiple enemies at random locations. I don’t know how to make only one AI spawn near the player at random times. If you could help give me an idea of what to look into, I would really appreciate it.

Thank you

---

<div class="post-metadata">

**Author:** ![mindsurferdev](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mindsurferdev](https://forums.unrealengine.com/u/mindsurferdev)\
**Post date:** [May 22, 2021, 12:25pm UTC](https://forums.unrealengine.com/t/spawning-one-enemy-near-the-player-at-random-times/483750/2 "2021-05-22T12:25:21Z")

</div>

Spawn actor from class on the (player’s location + Get the player camera’s forward vector and multiply it by  
negative DistanceFromThePlayer, except for the Z location). You can avoid the enemy from spawning behind a wall by line tracing to the spawn location from the player.

I can show you an image of how the BP would look if you need it

---

<div class="post-metadata">

**Author:** ![Catleopatra](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Catleopatra](https://forums.unrealengine.com/u/Catleopatra)\
**Post date:** [May 22, 2021, 12:34pm UTC](https://forums.unrealengine.com/t/spawning-one-enemy-near-the-player-at-random-times/483750/3 "2021-05-22T12:34:21Z")

</div>

Thank you for your reply.  
Yes, I would really appreciate it if I can see how it would look like.

---

<div class="post-metadata">

**Author:** ![mindsurferdev](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mindsurferdev](https://forums.unrealengine.com/u/mindsurferdev)\
**Post date:** [May 22, 2021, 12:42pm UTC](https://forums.unrealengine.com/t/spawning-one-enemy-near-the-player-at-random-times/483750/4 "2021-05-22T12:42:14Z")

</div>

![339472-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/b/7/bb7610f8b6cd4d365f7c8eda8d586afbe7af09a8.png)

And because I bet you will want this aswell. For the rotation of the enemy you can use a node called **Find look at rotation**.

---

<div class="post-metadata">

**Author:** ![Catleopatra](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Catleopatra](https://forums.unrealengine.com/u/Catleopatra)\
**Post date:** [May 22, 2021, 12:49pm UTC](https://forums.unrealengine.com/t/spawning-one-enemy-near-the-player-at-random-times/483750/5 "2021-05-22T12:49:17Z")

</div>

Thank you so much! I will work on making it spawn randomly.

---

<div class="post-metadata">

**Author:** ![mindsurferdev](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@mindsurferdev](https://forums.unrealengine.com/u/mindsurferdev)\
**Post date:** [May 22, 2021, 1:13pm UTC](https://forums.unrealengine.com/t/spawning-one-enemy-near-the-player-at-random-times/483750/6 "2021-05-22T13:13:27Z")

</div>

Sorry, I forgot the whole question after I started coding. I updated the code with everything you want.

 ![339474-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/c/4/7c4d513b11182bfd848be1b193b518ae68a1e4ad.png)

 ![339475-capture1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/0/6/f068a2efeddfbcdcc6f12837dbb38380bde6b87b.png)

 ![339476-capture2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/6/8/468b53f21e3a13d5306703aaa1be633c46ad9330.png)

So I start by setting a timer on event begin play, in the picture its between 0 and 30 seconds.

Then I line trace because the player could be standing with their back to a wall and you might not want the enemy to spawn behind the wall. In my case the enemy will spawn right behind the player if their back is right against a wall.

A line trace is a function that sweeps/moves from one position to another in one frame and if a X thing is in between those two positions it will get information about X thing. In my case I only get the hit location. More info here: [WTF Is? Line Trace For Objects in Unreal Engine 4 ( UE4 - YouTube](https://www.youtube.com/watch?v=6ZFnPliFSX0)

Ok so spawn the enemy after we know whether the players back is too close to a wall, make sure the enemy is facing the player with **Find look at rotation** and start the timer all over again at a new random time.

---

<div class="post-metadata">

**Author:** ![Catleopatra](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Catleopatra](https://forums.unrealengine.com/u/Catleopatra)\
**Post date:** [May 22, 2021, 3:59pm UTC](https://forums.unrealengine.com/t/spawning-one-enemy-near-the-player-at-random-times/483750/7 "2021-05-22T15:59:35Z")

</div>

Thank you, this has helped me a lot!
