So I try to spawn a projectile/bullet at the location of a muzzle of a gun. For this purpose, I created a socket located at the muzzle. Then, I spawn the projectile at the socket’s location. However, the projectile is spawned with a little offset. Not much, but way too much to be acceptable. I drew a debug sphere, and indeed, the location I get for the socket is incorrect in game for some reason. I have some images to show what I have done:
Socket in Editor: https://i.ibb.co/sy93sw9/socket-animation.png
Debug Sphere in Game: https://i.ibb.co/gSTG01W/debug-sphere.png
Blueprint Code to Draw Debug Sphere: https://i.ibb.co/FJKTChM/bp-code.png
I spawn the projectile with the following c++ code:
FVector SpawnLocation = SkeletalMesh->GetSocketLocation("BulletStartLocation");/*Owner->GetActorLocation();*/
FRotator SpawnRotation = SkeletalMesh->GetSocketRotation("BulletStartLocation");/* = Owner->GetActorRotation();*/
FTransform SpawnTransform = FTransform(SpawnRotation, SpawnLocation);
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("Projectile: %s"), *SpawnLocation.ToString()));
// Build spawn info.
FActorSpawnParameters SpawnInfo;
SpawnInfo.Instigator = OwnerPawn;
SpawnInfo.ObjectFlags |= RF_Transient;
SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
// Spawn projectile.
SpawnedProjectile = GetWorld()->SpawnActor<ARTSProjectile>(Attack.ProjectileClass, SpawnTransform, SpawnInfo);
I do not see any reason, why the socket location suddenly changes in game. Only reason I could see is that it is given in some slightly different coordinate system. However, the location of the socket appears to be in global coordinates, I checked the values. Otherwise, it should not be even remotely close to the gun, seeing that the gun carrying unit is walking across the map before shootingsomething.
Does anyone have an idea what might cause this behavior? Thank you!