Hello.
I have a Shooting System where the Projectile (rocket, grenade, etc…) spawns right where the Socket Location is.
In my case, my Character has this Socket Location for spawning projectiles:
The problem comes out when the Player is in front of a wall, like in this case:
You see that white character over there? When I shoot a rocket in that position, the rocket spawns **INSIDE **the wall and… it doesn’t explode!
Of course the Projectile items already have their Collision properties set up, and it works properly in “normal” cases.
But in this case, how can I overcome to this issue?
This is the code I use to spawn projectiles:
void AWeapon_Projectile::FireWeapon()
{
FVector SocketLocation = MyOwner->GetMesh()->GetSocketLocation((FName("WeaponSocket")));
FVector SocketRotation = MyOwner->GetMesh()->GetSocketRotation((FName("WeaponSocket"))).Vector();
FVector CameraLoc;
FRotator CameraRot;
MyOwner->GetActorEyesViewPoint(CameraLoc, CameraRot);
FVector Origin = SocketLocation;
FVector ShootDir = CameraRot.Vector();
// 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 - SocketRotation * 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);
}