When I try to get my camera for my line trace the engine crashes with an access violation why is this? If I try to line trace from the mesh or the spring arm it goes up and to the right in a weird direction.
Please copy your code using
Can’t read as it is.
FVector Start = PlayerCamera->GetComponentLocation();
FVector End = PlayerCamera->GetComponentLocation().ForwardVector + 1500.f;
FCollisionQueryParams Params;
Params.AddIgnoredActor(this);
DrawDebugLine(GetWorld(), Start, End, FColor(255, 0, 0, 1), false, 10.f, 2, 20);
return GetWorld()->LineTraceSingleByChannel(Hit, Start, End, ECollisionChannel::ECC_Camera, Params);
For how little I can see in the crash callstack, looks like PlayerCamera is nullptr.
If you could also copy your callstack, that would be great, but check and be sure PlayerCamera has a valid value.
here is my constructor where I create the camera
ASurvivalCharacter::ASurvivalCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
TurnRateGamepad = 45.f;
SurvivalStatsComp = CreateDefaultSubobject<USurvivalStatsComponent>("Survival Stats Component");
PlayerInventory = CreateDefaultSubobject<UPlayerInventory>("Player Inventory");
PlayerSpringArm = CreateDefaultSubobject<USpringArmComponent>("Player Spring Arm Camera");
PlayerSpringArm->TargetArmLength = 0;
PlayerSpringArm->bUsePawnControlRotation = true;
PlayerSpringArm->TargetArmLength = 500.f;
PlayerSpringArm->SetupAttachment(GetMesh());
PlayerCamera = CreateDefaultSubobject<UCameraComponent>("Player Camera");
PlayerCamera->SetupAttachment(PlayerSpringArm);
}
and the call for the line trace is here
FHitResult Hit;
bool bSuccess = bLineTrace(Hit);
if (bSuccess)
{
AActor* HitActor = Hit.GetActor();
if (HitActor != nullptr)
{
PlayerInventory->AddItemToInventory(HitActor);
}
UE_LOG(LogTemp, Warning, TEXT("Name is %s"), Hit.GetActor());
}