Sorry I know this has probably been answered before but I can’t find it for the life of me, tried google and searching here for a bunch of different related search terms.
I am using a third person camera on a spring arm, with the spring arm camera socket offset set to X 0, Y 60, Z 0 to give me a nice ‘over the shoulder’ perspective for third person. My issue is that using the rotation returned in GetActorEyesViewPoint() doesn’t compensate for the socket offset on the spring arm and my shots dont line up with the center of the screen, they’re slightly off to the left.
So I’m wondering if anyone has a good solution for this? I tried getting the camera component and using the component rotation vector and got all sorts of weird results.
GetActorEyesViewPoint(EyeLocation, EyeRotation);
FVector ShotDirection = EyeRotation.Vector();
FVector TraceEnd = EyeLocation + (ShotDirection * WeaponRange);
// LineTrace from EyeLocation to TraceEnd
You are on the right track. For third person games, I usually override my character’s GetPawnViewLocation function to return the camera component’s location instead. That would look something like this:
// MyCharacter.h
virtual FVector GetPawnViewLocation() const override;
// MyCharacter.cpp
FVector AMyCharacter::GetPawnViewLocation() const
{
if (MyCameraComponent)
{
// If my camera component is set, return its location instead.
return MyCameraComponent->GetComponentLocation();
}
// No camera component, return the default view location.
return Super::GetPawnViewLocation();
}
Your tracing code seems to be correct though.
// I'm assuming here that this code is in the character class. If not, you'll have to get
// the character somehow (through the player controller or something) and call
// GetActorEyesViewPoint on that actor.
FVector EyeLocation;
FRotator EyeRotation;
GetActorEyesViewPoint(EyeLocation, EyeRotation);
FVector ShotDirection = EyeRotation.Vector();
FVector TraceEnd = EyeLocation + (ShotDirection * 10000);
// Trace from EyeLocation to TraceEnd
Ahhh, overriding the GetPawnViewLocation() function sounds like a good solution, thanks will give it a try.
I did try getting camera component location and it still had a weird offset but I was using EyeRotation still. Gonna try with camera forward vector for ShotDirection and see if that helps.