How to convert FVector2D to FVector?

Hello everyone! I am just looking to convert an FVector2D to FVector in order to pass into the addactorlocaloffset() function. Is there a simple way to convert from FVector2D to FVector? In blueprint there is a function called getmouseY() I am trying to find a similar way of replicating the function so I can get the input

void APawnBlocker::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	
	float MouseX;
	float MouseY;
       //Here I am converting the mouse postion to FVector2D.
	FVector2D MouseXPos = UGameplayStatics::GetPlayerController(GetWorld(),0)->GetMousePosition(MouseX, MouseY);
        //This code below returns an error. Saying I need an FVector not FVector2D.
	FVector MouseVec(MouseXPos);

	AddActorLocaOffset(//Here I need an FVector in order for this function to operate.);
}

First, GetMousePosition() returns a bool not a Vector2D
The actual position is returned in the MouseX and MouseY variables.
So you can easily make a FVector as so:

	float MouseX;
	float MouseY;
	UGameplayStatics::GetPlayerController(GetWorld(),0)->GetMousePosition(MouseX, MouseY);
	FVector ToMove(MouseX, MouseY, 0);
	AddActorLocalOffset(ToMove);

Second, the mouse position is all positive (from 0 to width/height of screen) so the local offset you add will always be towards the positive axes forward (X) and right (Y) never the other direction.

Third, however – you almost never want to read the mouse directly like this.
Instead, you’ll want to map an input axis to the mouse X and mouse Y movements, and make the movement of your pawn be controlled by that input axis.

1 Like

An Fvector2d has only 2 float points, a regular fvector has 3 floating points.
So you need to fill in an extra floating point since the fvector has 3 channels.

It’s simple, just get the axis 2d chanels off of the 2d vectors as float points in two new float variables and then just add an extra new float variable for an extra channel , an extra float as zero, then fill your fvector with the new variables. You don’t even need an extra one , you can just say “0” inside the Fvector for the extra one, or if you need the xtra channel for whatever you can fill in whatever you like.

Your 2dvector to new float values, then your float values go into the new fvector. But as other user sugested maybe you need otherwise.

So . NewFloatVar = float point from your 2d vec dot your axis.
then you need another one just like the one above.
an axis can be X,Y,Z for example. Unreal has axis support built in the vectors just like open gl, so you can use specification on axis points, or you can specify positions 0 , 1 or 2, this is also supported in C++ with FVec

1 Like