Attach to Actor does not render the attached actor

Hello,

I’m currently using the ShooterGame project to implement a sticky bomb that would basically stick onto any character that it comes in contact with. I’ve managed to attach the bomb to a character as seen in the image below, but the bomb would not get rendered as soon as it get attached to the character.

259161-capture.png

This is the code snippet for attaching to the character.

SetActorLocation(HitResult.ImpactPoint);

const FAttachmentTransformRules AttachmentRules(EAttachmentRule::KeepRelative, true);

//AttachToComponent(OtherComponent, AttachmentRules, TraceResult.BoneName);
AttachToActor(OtherActor, AttachmentRules, TraceResult.BoneName);

I’ve confirmed that OtherComponent, OtherActor and the TraceResult.BoneName are valid data and not empty / null. I’ve considered the situation where the bomb would be hidden underneath the character’s mesh since the bomb is not that big, so I’ve modified the SetActorLocation(…) code above to:

TArray<UStaticMeshComponent*> StaticMeshes;
GetComponents<UStaticMeshComponent>(StaticMeshes);

for (const UStaticMeshComponent* Component : StaticMeshes)
{
	if (Component->GetName() == m_GrenadeMeshName)
	{
		m_GrenadeMeshBound = Component->Bounds.BoxExtent;
		break;
	}
}
....
const FVector NewPosition = TraceResult.ImpactPoint - GetActorForwardVector() * (m_GrenadeMeshBound);
SetActorLocation(NewPosition);

So basically I’ve gotten the extent of the grenade mesh in order to pull the grenade back out by the extent of the grenade, hoping that it will stick out from the character’s mesh but that didnt seem to solve anything.

I was wondering if anyone’s seen this behavior or can point out what I’m doing wrong with this. Thank you in advance!

Hi Kaltz,

I created a new project in 4.16 with the FPS Template to try to recreate this. I noticed that after I attached the projectile to the actor, the projectile kept moving because it kept simulating physics. That plus having to maintain a relative location to the target actor made the projectile jump in position then continue simulation. Have you tried setting the attachment rules to be “snap to target,” setting velocity to zero, stop simulating physics for the grenade, etc.?

Hey DarkWindRichard,

Thanks for getting back to me. I’ve thought about that and had already added the following code after attaching the projectile:

SetActorEnableCollision(false);
m_MovementComponent->StopMovementImmediately();
m_CollisionComponent->SetSimulatePhysics(false);

But that didnt change anything, but changing the attachment rule to SnapToTarget instead of KeepRelative as you suggested fixed the issue! I’m able to see the projectile stuck to the pawn.

But I’m seeing a new issue where the projectile is attached to the same location no matter where I shoot it from, as well as where it hits the pawn. I’ve checked the impact, point position, forward vector etc and checked that they werent all the same value.

I’ve tried changing EAttachmentRule::SnapToTarget to EAttachmentRule::KeepWorld and that seems to fix the issue of the projectiles not attaching to the “correct” position on the pawn. I was wondering if its okay to use KeepWorld as it seems to do what I want according to the comment on the enum and what I’m seeing is happening.

Hi Kaltz,

Yep! Using KeepWorld should be what you are looking for. That will keep the projectile’s world transform once the attachment has occurred, and then since it is attached to the actor at that point it will maintain that relative position to the actor.

For anyone who stumbles across this post, the issue I was having was not the FAttachmentTransformRules I was passing into the AttachToActor / AttachToComponent function. I’ve initialized the enums to EAttachmentRule::KeepRelative but instead, initializing it to EAttachmentRule::KeepWorld solved the issue for me.

Sweet, thank you very much for the help DarkwindRichard! I really appreciate it!