I see that you have accessed ViewProjectionMatrix. why dont you use ViewProjectionMatrix.TransformFVector4() function?. You might have to manipulate the retuned value to get the proper output
Hi Chris528! Thanks for sharing your code. I used the last updated version of your code from pastebin and it does what I need! I referred to your post in an other post (which I opened) and I also pasted your code there. Hope you don’t mind!
The performance improvement of manually multiplying the viewprojection matrix by the world position is most likely negligible, so you may want to use the TransformFVector4 method instead, as its much easier to read.
There is also a ConvertScreenLocationToWorldSpace & ConvertWorldLocationToScreenLocation that is callable from a PlayerController which works. These are not documented anywhere so it’s easy to miss them.
Note that these are the friendly blueprint function names and that if you want to call them from code it’s actually these functions:
/** Convert current mouse 2D position to World Space 3D position and direction. **/
UFUNCTION(BlueprintCallable, Category = "Game|Player", meta = (FriendlyName = "ConvertScreenLocationToWorldSpace"))
void DeprojectScreenPositionToWorld(float ScreenX, float ScreenY, FVector & WorldLocation, FVector & WorldDirection) const;
/**
* Convert a World Space 3D position into a 2D Screen Space position.
* @return true if the world coordinate was successfully projected to the screen.
*/
UFUNCTION(BlueprintCallable, Category = "Game|Player", meta = ( FriendlyName = "ConvertWorldLocationToScreenLocation" ))
bool ProjectWorldLocationToScreen(FVector WorldLocation, FVector2D& ScreenLocation) const;
Project world to screen function works only if it’s realy visible - if You want to do quest markers pin to screen edge, then it’s don’t work, and always return 0,0