# Project World to Screen Location stops returning a value

**URL:** <https://forums.unrealengine.com/t/project-world-to-screen-location-stops-returning-a-value/57135>\
**Category:** Blueprint\
**Tags:** unreal-engine\
**Created:** [March 3, 2016, 1:19pm UTC](https://forums.unrealengine.com/t/project-world-to-screen-location-stops-returning-a-value/57135 "2016-03-03T13:19:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martyn](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Martyn](https://forums.unrealengine.com/u/Martyn)\
**Post date:** [March 3, 2016, 1:19pm UTC](https://forums.unrealengine.com/t/project-world-to-screen-location-stops-returning-a-value/57135/1 "2016-03-03T13:19:10Z")

</div>

Hi,

I’m currently working on an blueprint implementation of [https://forums.unrealengine.com/showthread.php?59398-Easy-Offscreen-Indicator-Blueprint-Node](https://forums.unrealengine.com/showthread.php?59398-Easy-Offscreen-Indicator-Blueprint-Node)

I’ve managed to create a working script, however I’m trying to get my head around the ‘Project World To Screen’ node. It takes in a Player controller and a World Position (Vector 3) of the object you’re projecting and returns a Screen Position (Vector 2D). The object being projected is a ball. Whenever the ball is behind the player the Screen Position returns a zero vector. I’m not 100% sure why, but the node stops returning a value. I was thinking that it may be due to the fact that the ball at some point is no longer being rendered and thus the function returns a zero vector but this doesn’t make much sense since the world location of the ball is passed through continuously.

[![](https://i.imgur.com/JZRXXWg.png) ](https://i.imgur.com/JZRXXWg.png)

Is there an alternative way of calculating the Screen Position for when the object is behind the player?

**Here you can see the ball is slightly of screen:**

[![](https://i.imgur.com/m0VhgbB.png) ](https://i.imgur.com/m0VhgbB.png)

**Here once the player has turned so the ball is almost directly behind it the indicator disappears (because the Project World To Screen stops returning a Screen position):**

[![](https://i.imgur.com/WB6WVbO.png) ](https://i.imgur.com/WB6WVbO.png)

---

<div class="post-metadata">

**Author:** ![Martyn](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Martyn](https://forums.unrealengine.com/u/Martyn)\
**Post date:** [March 3, 2016, 1:29pm UTC](https://forums.unrealengine.com/t/project-world-to-screen-location-stops-returning-a-value/57135/2 "2016-03-03T13:29:57Z")

</div>

```auto

bool UGameplayStatics::ProjectWorldToScreen(APlayerController const* Player, const FVector& WorldPosition, FVector2D& ScreenPosition)
{
	ULocalPlayer* const LP = Player ? Player->GetLocalPlayer() : nullptr;
	if (LP && LP->ViewportClient)
	{
		// get the projection data
		FSceneViewProjectionData ProjectionData;
		if (LP->GetProjectionData(LP->ViewportClient->Viewport, eSSP_FULL, /*out*/ ProjectionData))
		{
			FMatrix const ViewProjectionMatrix = ProjectionData.ComputeViewProjectionMatrix();
			return FSceneView::ProjectWorldToScreen(WorldPosition, ProjectionData.GetConstrainedViewRect(), ViewProjectionMatrix, ScreenPosition);
		}
	}

	ScreenPosition = FVector2D::ZeroVector;
	return false;
}

```

This is the UE4 4.9.2 Build ‘Project World To Screen’ function in C++

---

<div class="post-metadata">

**Author:** ![Jambax](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jambax/32/904177_2.png) [@Jambax](https://forums.unrealengine.com/u/Jambax)\
**Post date:** [March 3, 2016, 1:31pm UTC](https://forums.unrealengine.com/t/project-world-to-screen-location-stops-returning-a-value/57135/3 "2016-03-03T13:31:57Z")

</div>

The default projection only works for objects that are in front of the camera’s direction vector, and unfortunately there’s no way to alter that in Blueprint.

I wrote a snippet of C++ which can handle reverse projections, which I use for screen-edge arrows, here’s the code. I’m going to put a Wiki tutorial up soon. ‘TargetWorldLocation’ is the position you want to transform.

```auto

		FVector Projected;
		bool bTargetBehindCamera = false;

		// Custom Projection Function
		ULocalPlayer* const LP = OwningGESPlayer->GetLocalPlayer();
		if (LP && LP->ViewportClient)
		{
			FSceneViewProjectionData NewProjectionData;
			if (LP->GetProjectionData(LP->ViewportClient->Viewport, EStereoscopicPass::eSSP_FULL, NewProjectionData))
			{
				const FMatrix ViewProjectionMatrix = NewProjectionData.ComputeViewProjectionMatrix();
				const FIntRect ViewRectangle = NewProjectionData.GetConstrainedViewRect();

				FPlane Result = ViewProjectionMatrix.TransformFVector4(FVector4(TargetWorldLocation, 1.f));
				if (Result.W < 0.f) { bTargetBehindCamera = true; }
				if (Result.W == 0.f) { Result.W = 1.f; } // Prevent Divide By Zero

				const float RHW = 1.f / FMath::Abs(Result.W);
				Projected = FVector(Result.X, Result.Y, Result.Z) * RHW;
	
				// Normalize to 0..1 UI Space
 				const float NormX = (Projected.X / 2.f) + 0.5f;
 				const float NormY = 1.f - (Projected.Y / 2.f) - 0.5f;
 
 				Projected.X = (float)ViewRectangle.Min.X + (NormX * (float)ViewRectangle.Width());
 				Projected.Y = (float)ViewRectangle.Min.Y + (NormY * (float)ViewRectangle.Height());
			}
		}

		FVector2D ScreenPos = FVector2D(Projected.X, Projected.Y);

```

---

<div class="post-metadata">

**Author:** ![Martyn](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Martyn](https://forums.unrealengine.com/u/Martyn)\
**Post date:** [March 3, 2016, 1:36pm UTC](https://forums.unrealengine.com/t/project-world-to-screen-location-stops-returning-a-value/57135/4 "2016-03-03T13:36:40Z")

</div>

Thank you for firstly fixing my post and also thank you very much for sharing your solution. I wonder if it will be addressed down the line. I will happily merge your function and test it.

---

<div class="post-metadata">

**Author:** ![gustavorios2](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/gustavorios2/32/1194641_2.png) [@gustavorios2](https://forums.unrealengine.com/u/gustavorios2)\
**Post date:** [September 21, 2016, 12:33pm UTC](https://forums.unrealengine.com/t/project-world-to-screen-location-stops-returning-a-value/57135/5 "2016-09-21T12:33:08Z")

</div>

> [@TheJamsh;486081](#):
>
> The default projection only works for objects that are in front of the camera’s direction vector, and unfortunately there’s no way to alter that in Blueprint.
> 
> I wrote a snippet of C++ which can handle reverse projections, which I use for screen-edge arrows, here’s the code. I’m going to put a Wiki tutorial up soon. ‘TargetWorldLocation’ is the position you want to transform.
> 
> ```auto
> 
> FVector Projected;
> bool bTargetBehindCamera = false;
> 
> // Custom Projection Function
> ULocalPlayer* const LP = OwningGESPlayer->GetLocalPlayer();
> if (LP && LP->ViewportClient)
> {
> FSceneViewProjectionData NewProjectionData;
> if (LP->GetProjectionData(LP->ViewportClient->Viewport, EStereoscopicPass::eSSP_FULL, NewProjectionData))
> {
> const FMatrix ViewProjectionMatrix = NewProjectionData.ComputeViewProjectionMatrix();
> const FIntRect ViewRectangle = NewProjectionData.GetConstrainedViewRect();
> 
> FPlane Result = ViewProjectionMatrix.TransformFVector4(FVector4(TargetWorldLocation, 1.f));
> if (Result.W < 0.f) { bTargetBehindCamera = true; }
> if (Result.W == 0.f) { Result.W = 1.f; } // Prevent Divide By Zero
> 
> const float RHW = 1.f / FMath::Abs(Result.W);
> Projected = FVector(Result.X, Result.Y, Result.Z) * RHW;
> 	
> // Normalize to 0..1 UI Space
> const float NormX = (Projected.X / 2.f) + 0.5f;
> const float NormY = 1.f - (Projected.Y / 2.f) - 0.5f;
>  
> Projected.X = (float)ViewRectangle.Min.X + (NormX * (float)ViewRectangle.Width());
> Projected.Y = (float)ViewRectangle.Min.Y + (NormY * (float)ViewRectangle.Height());
> }
> }
> 
> FVector2D ScreenPos = FVector2D(Projected.X, Projected.Y);
> 
> ```

I wish I know how to use this =(

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_2c79b30d](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_2c79b30d](https://forums.unrealengine.com/u/anonymous_user_2c79b30d)\
**Post date:** [February 17, 2017, 2:04am UTC](https://forums.unrealengine.com/t/project-world-to-screen-location-stops-returning-a-value/57135/6 "2017-02-17T02:04:17Z")

</div>

I wanted projections from behind the player also, so I dropped more or less what TheJamsh above has into a function that closely matches UE 4.14 ProjectWorldToScreen.

Declared:

```auto

UFUNCTION(BlueprintPure, Category = "UI Math")
static bool ProjectWorldToScreenBidirectional(APlayerController const* Player, const FVector& WorldPosition, FVector2D& ScreenPosition, bool& bTargetBehindCamera, bool bPlayerViewportRelative = false);

```

Defined:

```auto

bool UIBlueprintFunctionLibrary::ProjectWorldToScreenBidirectional(APlayerController const* Player, const FVector& WorldPosition, FVector2D& ScreenPosition, bool& bTargetBehindCamera, bool bPlayerViewportRelative)
{
	FVector Projected;
	bool bSuccess = false;

	ULocalPlayer* const LP = Player ? Player->GetLocalPlayer() : nullptr;
	if (LP && LP->ViewportClient)
	{
		// get the projection data
		FSceneViewProjectionData ProjectionData;
		if (LP->GetProjectionData(LP->ViewportClient->Viewport, eSSP_FULL, /*out*/ ProjectionData))
		{
			const FMatrix ViewProjectionMatrix = ProjectionData.ComputeViewProjectionMatrix();
			const FIntRect ViewRectangle = ProjectionData.GetConstrainedViewRect();

			FPlane Result = ViewProjectionMatrix.TransformFVector4(FVector4(WorldPosition, 1.f));
			if (Result.W < 0.f) { bTargetBehindCamera = true; }
			if (Result.W == 0.f) { Result.W = 1.f; } // Prevent Divide By Zero

			const float RHW = 1.f / FMath::Abs(Result.W);
			Projected = FVector(Result.X, Result.Y, Result.Z) * RHW;

			// Normalize to 0..1 UI Space
			const float NormX = (Projected.X / 2.f) + 0.5f;
			const float NormY = 1.f - (Projected.Y / 2.f) - 0.5f;

			Projected.X = (float)ViewRectangle.Min.X + (NormX * (float)ViewRectangle.Width());
			Projected.Y = (float)ViewRectangle.Min.Y + (NormY * (float)ViewRectangle.Height());

			bSuccess = true;
			ScreenPosition = FVector2D(Projected.X, Projected.Y);

			if (bPlayerViewportRelative)
			{
				ScreenPosition -= FVector2D(ProjectionData.GetConstrainedViewRect().Min);
			}
		}
		else
		{
			ScreenPosition = FVector2D(1234, 5678);
		}
	}

	return bSuccess;
}

```

Looks like:

![projection_node.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/6/5/5657b60ed9c291fb7f533754b4abe5587211d8a0.png)

---

<div class="post-metadata">

**Author:** ![Generica](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Generica](https://forums.unrealengine.com/u/Generica)\
**Post date:** [December 3, 2017, 9:28pm UTC](https://forums.unrealengine.com/t/project-world-to-screen-location-stops-returning-a-value/57135/8 "2017-12-03T21:28:14Z")

</div>

Can someone please explain, how do I add this to my editor?

---

<div class="post-metadata">

**Author:** ![daniel\_alves\_br](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@daniel\_alves\_br](https://forums.unrealengine.com/u/daniel_alves_br)\
**Post date:** [April 20, 2018, 9:04pm UTC](https://forums.unrealengine.com/t/project-world-to-screen-location-stops-returning-a-value/57135/9 "2018-04-20T21:04:45Z")

</div>

> [@Generica](#):
>
> Can someone please explain, how do I add this to my editor?

You need to create a new C++ class on Unreal Editor as Blueprint Function Library and save,  
then you add the first piece of code on the [name of your class].h and the second part on [name of your class].cpp

Save everything and compile on the editor.

---

<div class="post-metadata">

**Author:** ![daniel\_alves\_br](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@daniel\_alves\_br](https://forums.unrealengine.com/u/daniel_alves_br)\
**Post date:** [April 20, 2018, 9:07pm UTC](https://forums.unrealengine.com/t/project-world-to-screen-location-stops-returning-a-value/57135/10 "2018-04-20T21:07:18Z")

</div>

> [@Paul Schultz](#):
>
> I wanted projections from behind the player also, so I dropped more or less what TheJamsh above has into a function that closely matches UE 4.14 ProjectWorldToScreen.
> 
> Declared:
> 
> ```auto
> 
> UFUNCTION(BlueprintPure, Category = "UI Math")
> static bool ProjectWorldToScreenBidirectional(APlayerController const* Player, const FVector& WorldPosition, FVector2D& ScreenPosition, bool& bTargetBehindCamera, bool bPlayerViewportRelative = false);
> 
> ```
> 
> Defined:
> 
> ```auto
> 
> bool UIBlueprintFunctionLibrary::ProjectWorldToScreenBidirectional(APlayerController const* Player, const FVector& WorldPosition, FVector2D& ScreenPosition, bool& bTargetBehindCamera, bool bPlayerViewportRelative)
> {
> FVector Projected;
> bool bSuccess = false;
> 
> ULocalPlayer* const LP = Player ? Player->GetLocalPlayer() : nullptr;
> if (LP && LP->ViewportClient)
> {
> // get the projection data
> FSceneViewProjectionData ProjectionData;
> if (LP->GetProjectionData(LP->ViewportClient->Viewport, eSSP_FULL, /*out*/ ProjectionData))
> {
> const FMatrix ViewProjectionMatrix = ProjectionData.ComputeViewProjectionMatrix();
> const FIntRect ViewRectangle = ProjectionData.GetConstrainedViewRect();
> 
> FPlane Result = ViewProjectionMatrix.TransformFVector4(FVector4(WorldPosition, 1.f));
> if (Result.W < 0.f) { bTargetBehindCamera = true; }
> if (Result.W == 0.f) { Result.W = 1.f; } // Prevent Divide By Zero
> 
> const float RHW = 1.f / FMath::Abs(Result.W);
> Projected = FVector(Result.X, Result.Y, Result.Z) * RHW;
> 
> // Normalize to 0..1 UI Space
> const float NormX = (Projected.X / 2.f) + 0.5f;
> const float NormY = 1.f - (Projected.Y / 2.f) - 0.5f;
> 
> Projected.X = (float)ViewRectangle.Min.X + (NormX * (float)ViewRectangle.Width());
> Projected.Y = (float)ViewRectangle.Min.Y + (NormY * (float)ViewRectangle.Height());
> 
> bSuccess = true;
> ScreenPosition = FVector2D(Projected.X, Projected.Y);
> 
> if (bPlayerViewportRelative)
> {
> ScreenPosition -= FVector2D(ProjectionData.GetConstrainedViewRect().Min);
> }
> }
> else
> {
> ScreenPosition = FVector2D(1234, 5678);
> }
> }
> 
> return bSuccess;
> }
> 
> ```
> 
> Looks like:
> 
> ![projection_node.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/6/5/5657b60ed9c291fb7f533754b4abe5587211d8a0.png)

I suggest you to add na ELSE parameter to set bTargetBehindCamera back to false when look to the tag point again.

if (Result.W \< 0.f) { bTargetBehindCamera = true; } else {bTargetBehindCamera = false;}

---

<div class="post-metadata">

**Author:** ![BrandenMarais](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BrandenMarais](https://forums.unrealengine.com/u/BrandenMarais)\
**Post date:** [January 3, 2024, 7:52am UTC](https://forums.unrealengine.com/t/project-world-to-screen-location-stops-returning-a-value/57135/11 "2024-01-03T07:52:50Z")

</div>

Just an update for anyone not getting this to work in UE5.

Code for the .h

```auto
UFUNCTION(BlueprintPure, Category = "UI Math")
static bool ProjectWorldToScreenBidirectional(APlayerController const* Player, const FVector& WorldPosition, FVector2D& ScreenPosition, bool& bTargetBehindCamera, bool bPlayerViewportRelative = false);

```

Code for the .cpp

```auto
bool UIBlueprintFunctionLibrary::ProjectWorldToScreenBidirectional(APlayerController const* Player, const FVector& WorldPosition, FVector2D& ScreenPosition, bool& bTargetBehindCamera, bool bPlayerViewportRelative)
{
	FVector Projected;
	bool bSuccess = false;

	ULocalPlayer* const LP = Player ? Player->GetLocalPlayer() : nullptr;
	if (LP && LP->ViewportClient)
	{
		// get the projection data
		FSceneViewProjectionData ProjectionData;
		if (LP->GetProjectionData(LP->ViewportClient->Viewport, /*out*/ ProjectionData))
		{
			const FMatrix ViewProjectionMatrix = ProjectionData.ComputeViewProjectionMatrix();
			const FIntRect ViewRectangle = ProjectionData.GetConstrainedViewRect();

			FPlane Result = ViewProjectionMatrix.TransformFVector4(FVector4(WorldPosition, 1.f));
			if (Result.W < 0.f) { bTargetBehindCamera = true; } else { bTargetBehindCamera = false; }
			if (Result.W == 0.f) { Result.W = 1.f; } // Prevent Divide By Zero

			const float RHW = 1.f / FMath::Abs(Result.W);
			Projected = FVector(Result.X, Result.Y, Result.Z) * RHW;

			// Normalize to 0..1 UI Space
			const float NormX = (Projected.X / 2.f) + 0.5f;
			const float NormY = 1.f - (Projected.Y / 2.f) - 0.5f;

			Projected.X = (float)ViewRectangle.Min.X + (NormX * (float)ViewRectangle.Width());
			Projected.Y = (float)ViewRectangle.Min.Y + (NormY * (float)ViewRectangle.Height());

			bSuccess = true;
			ScreenPosition = FVector2D(Projected.X, Projected.Y);

			if (bPlayerViewportRelative)
			{
				ScreenPosition -= FVector2D(ProjectionData.GetConstrainedViewRect().Min);
			}
		}
		else
		{
			ScreenPosition = FVector2D(1234, 5678);
		}
	}

	return bSuccess;
}

```

Changed:

```auto
if (LP->GetProjectionData(LP->ViewportClient->Viewport, eSSP_FULL, /*out*/ ProjectionData))

```

**To**

```auto
if (LP->GetProjectionData(LP->ViewportClient->Viewport, /*out*/ ProjectionData))

```

And added Daniel\_Alves\_br’s suggestion.

> I suggest you to add na ELSE parameter to set bTargetBehindCamera back to false when look to the tag point again.
> 
> ```auto
> if (Result.W < 0.f) { bTargetBehindCamera = true; } else {bTargetBehindCamera = false;}
> 
> ```

---

<div class="post-metadata">

**Author:** ![TheNameIsHush1](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/thenameishush1/32/953321_2.png) [@TheNameIsHush1](https://forums.unrealengine.com/u/TheNameIsHush1)\
**Post date:** [July 1, 2024, 6:23pm UTC](https://forums.unrealengine.com/t/project-world-to-screen-location-stops-returning-a-value/57135/12 "2024-07-01T18:23:56Z")

</div>

> [@BrandenMarais](#):
>
> `ProjectWorldToScreenBidirectional`

@BrandenMarais I seem to be running into a weird issue when the camera is infront of my world position:  
https://youtu.be/5PHJEjvI9oI

I am using your function and was wondering if you had any idea on why the screen position would be swaying right when moving infront of the world position? And I was also wondering if I could see how you use the function for off screen indicators?
