# How to spawn actors at edge of viewport?

**URL:** <https://forums.unrealengine.com/t/how-to-spawn-actors-at-edge-of-viewport/398506>\
**Category:** Cinematics & Media\
**Tags:** screen-space, world-space, unreal-engine, Blueprint, question\
**Created:** [June 8, 2017, 10:57pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actors-at-edge-of-viewport/398506 "2017-06-08T22:57:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_a45b4ad0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a45b4ad0](https://forums.unrealengine.com/u/anonymous_user_a45b4ad0)\
**Post date:** [June 8, 2017, 10:57pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actors-at-edge-of-viewport/398506/1 "2017-06-08T22:57:05Z")

</div>

Hi, I’d like to spawn actors at the edges of a camera’s viewport, so that it can barely “see” them.  
I am using the Top Down template, so it’s a perspective camera. Below is a simplified drawing of what it should look like:

![200175-skizze.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/2/2/f22221f7cabf44b34adfd14e2309dba08011810f.png)

Actors should be spawned at the locations where the circles are.  
As I know the screen coordinates of these points, I tried putting them into “deproject from screen to world”.  
The resulting world coordinates are all very close to the bottom circle.  
Any idea how to solve this?

---

<div class="post-metadata">

**Author:** ![NoobsDeSroobs](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@NoobsDeSroobs](https://forums.unrealengine.com/u/NoobsDeSroobs)\
**Post date:** [June 8, 2017, 11:02pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actors-at-edge-of-viewport/398506/2 "2017-06-08T23:02:30Z")

</div>

Will you camera move? Will the character never stray from the center? An easy way to approach it is to get the character location and add 4 Vectors that point out to the 4 circles.

If you want to get more fancyyou have to store 4 directional unit vectors and use some calculations based on the distance between the camera and the character, and the FOV etc, to calculate how LONG the vectors needs to be.

HTH

---

<div class="post-metadata">

**Author:** ![redbox](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@redbox](https://forums.unrealengine.com/u/redbox)\
**Post date:** [June 9, 2017, 6:14am UTC](https://forums.unrealengine.com/t/how-to-spawn-actors-at-edge-of-viewport/398506/3 "2017-06-09T06:14:39Z")

</div>

In world coordinates, screen is a small rectangle near the camera. Its size and location depends on near clip plane distance, fov and aspect ratio. So deproject function returns location that is almost equal to camera location.

Real world location of screen edge depends on its depth.

In case your game is a kind of scroller, your character (and enemies) will move in some constant depth from camera.

Usually, NearClipPlaneDist = 10. This is depth of the screen, and its world location will be:

```
ScreenWorldLocation = CameraLocation + CameraForwardVector * Depth.

```

Replace Depth with any value, and you will get world location in this depth.

World screen size calculated like:

```
ScreenY = 2 * Depth * Atan (FOV * 0.5)

ScreenX = ScreenY * AspectRatio

```

In case your character not moves in constant depth (terrain, slopes, ladders), it is better to make line trace from camera location in 4 directions of screen edges. It should be calculated like:

```
Normalize(CameraLocation - (ScreenWorldLocation + ScreenY * 0.5))

Normalize(CameraLocation - (ScreenWorldLocation + ScreenX * 0.5))

Normalize(CameraLocation - (ScreenWorldLocation + ScreenY * (-0.5)))

Normalize(CameraLocation - (ScreenWorldLocation + ScreenX * (-0.5)))

```

Or something like that.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a45b4ad0](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a45b4ad0](https://forums.unrealengine.com/u/anonymous_user_a45b4ad0)\
**Post date:** [June 9, 2017, 12:48pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actors-at-edge-of-viewport/398506/4 "2017-06-09T12:48:40Z")

</div>

Thanks, that pointed me in the right direction!  
I solved it doing a LineTrace with  
`start = camera location` and  
`end = end point of extended vector from camera location to deprojected point on near plane`

I get world coordinates on the floor now, which is what i wanted.

I kinda wanted something simillar to this:  
[A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums](https://wiki.unrealengine.com/Get_Camera_Edges_From_Frustum) in order to achieve something like this:  
[Create bouncy collision bounds matching view frustum - World Creation - Epic Developer Community Forums](https://forums.unrealengine.com/showthread.php?124150-Create-bouncy-collision-bounds-matching-view-frustum)

---

<div class="post-metadata">

**Author:** ![silver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@silver](https://forums.unrealengine.com/u/silver)\
**Post date:** [December 7, 2018, 9:33am UTC](https://forums.unrealengine.com/t/how-to-spawn-actors-at-edge-of-viewport/398506/5 "2018-12-07T09:33:50Z")

</div>

would you mind showing me your solution, i am running into issues trying to do the same.

---

<div class="post-metadata">

**Author:** ![Mateusz\_Fryc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mateusz\_Fryc](https://forums.unrealengine.com/u/Mateusz_Fryc)\
**Post date:** [August 25, 2020, 12:09pm UTC](https://forums.unrealengine.com/t/how-to-spawn-actors-at-edge-of-viewport/398506/6 "2020-08-25T12:09:17Z")

</div>

Hi guys, I would like to drop here my solution. Below method calculates four vertices of camera facing rectangle in world space, that is aligned with viewport edges and fills it entirely. You can specify distance at which you would like to have your rectangle calculated. I’ve added this method to my camera actor.

The default distance of 100u is the currently predefined distance of near clipping plane. Offsets allow to scale rectangle, which helped me to get rectangle slightly bigger than viewport, which I needed to safely fit backgrounds of my 2d game within viewport.

With this you can check if any of your actors are outside of the rectangle bounds on it’s plane at any given distance from camera.

```
void AiCameraActor::GetViewportSizedRectAtDistance( FVector OutputArr[], float Distance = 100.0f, float HorizontalOffset = 0.0f, float VerticalOffset = 0.0f )
{
	FMinimalViewInfo Info = FMinimalViewInfo();
	GetCameraComponent()->GetCameraView( GetWorld()->GetDeltaSeconds(), Info );
	FVector LeftVector( 0.0f, -1.0f, 0.0f );
	FVector UpVector( 0.0f, 0.0f, 1.0f );
	FVector LookDirection = Info.Rotation.Vector();
	LookDirection.Normalize();
	FVector2D ViewportSize = FVector2D( 0.f, 0.f );
	GEngine->GameViewport->GetViewportSize(ViewportSize);
	const float HalfHorizontalFOVInRadians = FMath::DegreesToRadians( Info.FOV * 0.5f );
	float FrustumAspectRatio = Info.bConstrainAspectRatio ? Info.AspectRatio : ViewportSize.X / ViewportSize.Y;
	float HozLength = 0.0f;
	float VertLength = 0.0f;

	HozLength = Distance * FMath::Tan(HalfHorizontalFOVInRadians) + HorizontalOffset;
	VertLength = HozLength / FrustumAspectRatio + VerticalOffset;

	FVector ViewportWorldCetnter = Info.Location + LookDirection * Distance;
	FVector LeftLength = LeftVector * HozLength;
	FVector UpLength = UpVector * VertLength;

	OutputArr[0] = ViewportWorldCetnter - UpLength - LeftLength;
	OutputArr[1] = ViewportWorldCetnter - UpLength + LeftLength;
	OutputArr[2] = ViewportWorldCetnter + UpLength + LeftLength;
	OutputArr[3] = ViewportWorldCetnter + UpLength - LeftLength;
}
```
