Currently trying to spawn a projectile at gun’s static mesh’s muzzle socket location. However, it has resulted in the projectile being spawned at the player character’s foot like shown here
Here’s a picture of the muzzle socket in the static mesh
And here’s the code for spawning the projectile,
for (int i = 0; i < Components.Num(); i++) {
if (Components[i]->GetName() == "Weapon") {
UStaticMeshComponent* WeaponComponent = Components[i];
FVector MuzzleLocation = WeaponComponent->GetSocketLocation("muzzle");
APlayerController* PlayerController = Cast<APlayerController>(GetController());
FRotator CameraRotation = PlayerController->PlayerCameraManager->GetCameraRotation();
FVector CameraLocation = PlayerController->PlayerCameraManager->GetCameraLocation();
PlayerController->GetPlayerViewPoint(CameraLocation, CameraRotation);
FVector ShootDirection = CameraRotation.Vector();
const FVector StartTrace = FollowCamera->GetComponentLocation();
const FVector EndTrace = StartTrace + ShootDirection * 100000.0;
FHitResult Impact;
FCollisionQueryParams CollisionParams;
GetWorld()->LineTraceSingleByChannel(Impact, StartTrace, EndTrace, ECC_Visibility, CollisionParams);
if (Impact.bBlockingHit) {
FTransform SpawnTransform(ShootDirection.Rotation(), MuzzleLocation);
auto Bullet = Cast<AProjectileActor>(UGameplayStatics::BeginDeferredActorSpawnFromClass(this, CurrentWeapon->BulletClass, SpawnTransform));
if (Bullet != nullptr)
{
//spawnactor has no way of passing parameters so need to use begindeferredactorspawn and finishspawningactor
Bullet->Weapon = CurrentWeapon;
Bullet->WeaponHolder = this;
UGameplayStatics::FinishSpawningActor(Bullet, SpawnTransform);
}
}
break;
}
}
Not sure what’s causing this, any help would be appreciated!
EDIT
The code above is in a server rpc. Oddly enough, the bullet spawns on the muzzle socket on the server, but when replicating this back to the client the location is incorrect. I’ve also experienced a similar issue with AttachtToComponent
attaching in different rotations on server vs client.