# Calling SpawnActor() using GetSocketLocation() does not spawn the actor at the right location

**URL:** <https://forums.unrealengine.com/t/calling-spawnactor-using-getsocketlocation-does-not-spawn-the-actor-at-the-right-location/448408>\
**Category:** Multiplayer & Networking\
**Tags:** Replication, spawnactor, unreal-engine, Multiplayer, question, CPP\
**Created:** [May 5, 2019, 3:48am UTC](https://forums.unrealengine.com/t/calling-spawnactor-using-getsocketlocation-does-not-spawn-the-actor-at-the-right-location/448408 "2019-05-05T03:48:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris-gong](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chris-gong/32/1106462_2.png) [@chris-gong](https://forums.unrealengine.com/u/chris-gong)\
**Post date:** [May 5, 2019, 3:48am UTC](https://forums.unrealengine.com/t/calling-spawnactor-using-getsocketlocation-does-not-spawn-the-actor-at-the-right-location/448408/1 "2019-05-05T03:48:03Z")

</div>

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

[![](https://d3kjluh73b9h9o.cloudfront.net/original/3X/2/0/20002faf28adfd94ca98cf6ced46f14334b53684.png) ](https://i.imgur.com/removed.png)

Here’s a picture of the muzzle socket in the static mesh

[![](https://d3kjluh73b9h9o.cloudfront.net/original/3X/2/0/20002faf28adfd94ca98cf6ced46f14334b53684.png) ](https://i.imgur.com/removed.png)

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.

---

<div class="post-metadata">

**Author:** ![Lardo\_Deepdelver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lardo\_Deepdelver](https://forums.unrealengine.com/u/Lardo_Deepdelver)\
**Post date:** [May 5, 2019, 8:31am UTC](https://forums.unrealengine.com/t/calling-spawnactor-using-getsocketlocation-does-not-spawn-the-actor-at-the-right-location/448408/2 "2019-05-05T08:31:46Z")

</div>

Have you tried drawing a debug sphere at the MuzzleLocation just to test if that one is correct.

that would be that

> **[DrawDebugSphere](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/DrawDebugSphere/)**
>
> Draw a debug sphere

other things I see is that you don’t set the ESpawnActorCollisionHandlingMethod for BeginDeferredActorSpawnFromClass which you should set to AlwaysSpawn so that the position doesn’t get changed on spawn.

---

<div class="post-metadata">

**Author:** ![henryLiu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@henryLiu](https://forums.unrealengine.com/u/henryLiu)\
**Post date:** [May 5, 2019, 11:28pm UTC](https://forums.unrealengine.com/t/calling-spawnactor-using-getsocketlocation-does-not-spawn-the-actor-at-the-right-location/448408/3 "2019-05-05T23:28:15Z")

</div>

Failed to GetSocketTransform from weapon, so the Bullet spawned on ZeroVector.

---

<div class="post-metadata">

**Author:** ![Firefly74](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Firefly74](https://forums.unrealengine.com/u/Firefly74)\
**Post date:** [May 6, 2019, 2:56pm UTC](https://forums.unrealengine.com/t/calling-spawnactor-using-getsocketlocation-does-not-spawn-the-actor-at-the-right-location/448408/4 "2019-05-06T14:56:20Z")

</div>

+1, you should log the MuzzleLocation value ( or put breakpoint ) to check the returned value

THe weapons doesn’ tlooks like to be the same, make sure all meshes have the socket setup

---

<div class="post-metadata">

**Author:** ![chris-gong](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chris-gong/32/1106462_2.png) [@chris-gong](https://forums.unrealengine.com/u/chris-gong)\
**Post date:** [May 6, 2019, 5:27pm UTC](https://forums.unrealengine.com/t/calling-spawnactor-using-getsocketlocation-does-not-spawn-the-actor-at-the-right-location/448408/5 "2019-05-06T17:27:05Z")

</div>

I have tried doing drawdebugsphere and the bullet spawns on the socket location on the server but not on the clients. This code is currently in a server rpc. And weirdly enough it does spawn normally in a single player game. Do you think there’s a problem with spawning things server side?

---

<div class="post-metadata">

**Author:** ![chris-gong](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chris-gong/32/1106462_2.png) [@chris-gong](https://forums.unrealengine.com/u/chris-gong)\
**Post date:** [May 6, 2019, 5:29pm UTC](https://forums.unrealengine.com/t/calling-spawnactor-using-getsocketlocation-does-not-spawn-the-actor-at-the-right-location/448408/6 "2019-05-06T17:29:45Z")

</div>

Unfortunately I did try logging the muzzle location value and it’s not a zero vector. However, one thing i found out is that the bullet spawns at the socket location in a single player game but not in a multiplayer game. This code is in a server rpc currently. Do you think there’s something wrong with spawning server side? Because on the server it spawns fine but it doesn’t replicate currently to the clients.

---

<div class="post-metadata">

**Author:** ![henryLiu](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@henryLiu](https://forums.unrealengine.com/u/henryLiu)\
**Post date:** [May 6, 2019, 10:30pm UTC](https://forums.unrealengine.com/t/calling-spawnactor-using-getsocketlocation-does-not-spawn-the-actor-at-the-right-location/448408/7 "2019-05-06T22:30:48Z")

</div>

Anyway, the code you post looks like suit only for local running. If it is in server rpc as you said, you may need to SetOwner of the bullet.

Normally, using a local function to calculate the start pose of bullet, and call a server function to spawn bullet with certain pose, would be suit for most of needs of sync-bullet. ShooterGame could be a good reference for you.

---

<div class="post-metadata">

**Author:** ![chris-gong](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chris-gong/32/1106462_2.png) [@chris-gong](https://forums.unrealengine.com/u/chris-gong)\
**Post date:** [May 24, 2019, 3:08pm UTC](https://forums.unrealengine.com/t/calling-spawnactor-using-getsocketlocation-does-not-spawn-the-actor-at-the-right-location/448408/8 "2019-05-24T15:08:33Z")

</div>

Hi Henry, sorry for the late response. It seems that SetOwner doesn’t change the result either. However, when you say use a local function to calculate the start pose, then call a server rpc to spawn the bullet, how would I pass the start pose from the local client function to the server rpc? Thanks for your help so far.

---

<div class="post-metadata">

**Author:** ![chris-gong](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chris-gong/32/1106462_2.png) [@chris-gong](https://forums.unrealengine.com/u/chris-gong)\
**Post date:** [May 25, 2019, 1:36pm UTC](https://forums.unrealengine.com/t/calling-spawnactor-using-getsocketlocation-does-not-spawn-the-actor-at-the-right-location/448408/9 "2019-05-25T13:36:23Z")

</div>

Nevermind I’m stupid, for some reason I thought you couldn’t pass parameters to server rpcs. Calculating the location client side did work. Thank you henry.
