How would I get FirstPersonCameraComponent->GetForwardVector(); from another class? The player (PlayerBase.cpp) calls the function Fire(); located in WeaponBase.cpp and it will shoot either a line trace or a projectile depending on enum. I’m attempting to get the forward vector from the camera component which is attached to the player but I am unsure how I would access the cameracomponent through WeaponBase.cpp.
My Code:
WeaponBase.CPP
// Fill out your copyright notice in the Description page of Project Settings.
#include "WeaponBase.h"
#inlcude "PlayerBase.h"
#include "Components/BoxComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "Engine.h"
// Sets default values
AWeaponBase::AWeaponBase()
{
CollisionComp = CreateDefaultSubobject<UBoxComponent>(TEXT("CollisionComp"));
RootComponent = CollisionComp;
WeaponMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("WeaponMesh"));
WeaponMesh -> SetupAttachment(RootComponent);
}
void AWeaponBase::Fire()
{
if (ProjectileType == EWeaponProjectile::EBullet)
{
InstantFire();
}
if (ProjectileType == EWeaponProjectile::ESpread)
{
for (int32 i = 0; i <= WeaponConfig.ShotNum; i++)
{
InstantFire();
}
}
if (ProjectileType == EWeaponProjectile::EProjectile)
{
}
}
void AWeaponBase::InstantFire()
{
const int32 RandomSeed = FMath::Rand();
FRandomStream WeaponRandomStream(RandomSeed);
const float CurrentSpread = WeaponConfig.WeaponSpread;
const float SpreadCone = FMath::DegreesToRadians(WeaponConfig.WeaponSpread * 0.5);
const FVector AimDir =
const FVector StartTrace = WeaponMesh->GetSocketLocation("MF");
const FVector ShootDir = WeaponRandomStream.VRandCone(AimDir, SpreadCone);
const FVector EndTrace = StartTrace + ShootDir * WeaponConfig.WeaponRange;
const FHitResult Impact = WeaponTrace(StartTrace, EndTrace);
ProcessInstantHit(Impact, StartTrace, ShootDir, RandomSeed, CurrentSpread);
}
FHitResult AWeaponBase::WeaponTrace(const FVector& TraceFrom, const FVector& TraceTo) const
{
static FName WeaponFireTag = FName(TEXT("WeaponTrace"));
FCollisionQueryParams TraceParams(WeaponFireTag, true, Instigator);
TraceParams.bReturnPhysicalMaterial = true;
TraceParams.AddIgnoredActor(this);
FHitResult Hit(ForceInit);
GetWorld()->LineTraceSingleByChannel(Hit, TraceFrom, TraceTo, TRACE_WEAPON, TraceParams);
return Hit;
}
void AWeaponBase::ProcessInstantHit(const FHitResult& Impact, const FVector& Origin, const FVector& ShootDir, int32 RandomSeed, float ReticleSpread)
{
const FVector EndTrace = Origin + ShootDir * WeaponConfig.WeaponRange;
const FVector EndPoint = Impact.GetActor() ? Impact.ImpactPoint : EndTrace;
DrawDebugLine(this->GetWorld(), Origin, Impact.TraceEnd, FColor::Red, true, 10000, 10.f);
}