I having been playing around with the engine and creating a sample multiplayer game. I wanted to make a projectile, and i knew the shooter game sample has one. I was digging around in there, and i came across this function:
void AShooterWeapon_Projectile::FireWeapon()
{
FVector ShootDir = GetAdjustedAim();
FVector Origin = GetMuzzleLocation();
// trace from camera to check what's under crosshair
const float ProjectileAdjustRange = 10000.0f;
const FVector StartTrace = GetCameraDamageStartLocation(ShootDir);
const FVector EndTrace = StartTrace + ShootDir * ProjectileAdjustRange;
FHitResult Impact = WeaponTrace(StartTrace, EndTrace);
// and adjust directions to hit that actor
if (Impact.bBlockingHit)
{
const FVector AdjustedDir = (Impact.ImpactPoint - Origin).GetSafeNormal();
bool bWeaponPenetration = false;
const float DirectionDot = FVector::DotProduct(AdjustedDir, ShootDir);
if (DirectionDot < 0.0f)
{
// shooting backwards = weapon is penetrating
bWeaponPenetration = true;
}
else if (DirectionDot < 0.5f)
{
// check for weapon penetration if angle difference is big enough
// raycast along weapon mesh to check if there's blocking hit
FVector MuzzleStartTrace = Origin - GetMuzzleDirection() * 150.0f;
FVector MuzzleEndTrace = Origin;
FHitResult MuzzleImpact = WeaponTrace(MuzzleStartTrace, MuzzleEndTrace);
if (MuzzleImpact.bBlockingHit)
{
bWeaponPenetration = true;
}
}
if (bWeaponPenetration)
{
// spawn at crosshair position
Origin = Impact.ImpactPoint - ShootDir * 10.0f;
}
else
{
// adjust direction to hit
ShootDir = AdjustedDir;
}
}
ServerFireProjectile(Origin, ShootDir);
}
My question is this, i am confused as to what the IF statement of
"if (Impact.bBlockingHit){
…
}"
is doing. There are statements like “shooting backwards = weapon is penetrating” in that block of code, how can a weapon be shooting backwards?
I am not really sure i understand why there is a weapon trace for a projectile either. I get why you would need on for an instant hit weapon, but for something like a launcher i am confused why the trace is needed.
If someone could maybe explain the need for that IF statement and what game play purpose is it solving, and why there is a trace for a projectile weapon I would appreciate it!