How to spawn actors at edge of viewport?

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

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.

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 in order to achieve something like this:
Create bouncy collision bounds matching view frustum - World Creation - Epic Developer Community Forums

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

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?

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

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;
}