Download

Move a vector along with camera

Hello,
I’m building the line trace for a weapon in c++.
My line trace starts at my camera position and ends further away.

I store a vector of where the first shot landed (the origin of the burst), because the location where the other shots will land will depend on that origin.

However, if I move the camera, I need the origin to move along with it, how can I do that?

thanks!

When the origin position is first defined, store the difference between the origin position and the camera position in a variable.

Then, the origin can be recalculated whenever you want be just adding that previously stored difference with the current camera position.

Hope this helps!

1 Like

Hello NachoMonkey, so I managed to do that but now I’m facing another issue. My current setup is using the event tick inside my weapon, that happens before the Input events (when I move the camera) inside my pawn:

I have declared a class variable inside my weapon: BurstOrigin, to hold the vector of where my first shot landed.

Whenever I take my first shot, I set the value of the origin:

BurstOrigin = GetAimLocation();

GetAimLocation is the point where I’m aiming right now (based on the camera position of the pawn holding the weapon).

Then I created another variable inside my weapon to update where I’m aiming every frame:

void APRT_Gun_Hitscan::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);
	AimLocLastFrame = GetAimLocation();
}

AddControllerPitchInput and AddControllerYawInput are functions inside the pawn that are called every frame too. Whenever their parameter is != 0 that means my camera is moving, so I need to move the origin too. I then get the AimLocLastFrame variable and subtract from where my camera currently is aiming, to get the difference vector, then I add it to the BurstOrigin.

void APRT_Character::AddControllerPitchInput(float Val)
{
	Super::AddControllerPitchInput(Val);
	
	APRT_Gun_Hitscan* GunHitscan = Cast<APRT_Gun_Hitscan>(CurrentItemInHand);

	if(GunHitscan && Val != 0.0f)
	{
			 FVector CurrentLoc = GetAimLocation();
	         BurstOrigin += (CurrentLoc - AimLocLastFrame);
	         AimLocLastFrame = GetAimLocation(); // the tick inside the weapon has already happened, so update the last frame information again to keep it up-to-date with the changes
	}
}

void APRT_Character::AddControllerYawInput(float Val)
{
	Super::AddControllerYawInput(Val);
		
	APRT_Gun_Hitscan* GunHitscan = Cast<APRT_Gun_Hitscan>(CurrentItemInHand);

	if(GunHitscan && Val != 0.0f)
	{
			 FVector CurrentLoc = GetAimLocation();
	         BurstOrigin += (CurrentLoc - AimLocLastFrame);
	         AimLocLastFrame = GetAimLocation(); // the tick inside the weapon has already happened, so update the last frame information again to keep it up-to-date with the changes
	}
}

So basically every frame I’m checking if any input was added to the camera, if it was, I get where the camera was last frame and subtract from where it is now, to get the vector that is the difference between them, then I add the difference to the origin.

That works flawlessly if I move the mouse fast, however if I move it slowly, it remains the same.
I’ve recorded a video showing the problem, I drew a debug line from my character to the origin every frame so you can see how it is: Untitled - YouTube

I tested it with different framerates and it seems that when the framerate is lower the problem happens when I move the mouse faster, but it stops if I move it slower (the opposite). Do you have any ideas what is causing this? thanks for your help!