Weird Result from Attach Component to socket

Hello,

I want to be able to attach the projectile that I’ve fired into any enemy actor that is hit

I’m using first person c++ sample code to achieve this

This is the code that I used

void AEnemy::ReceiveHit(class UPrimitiveComponent* MyComp, class AActor* other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit)
{
	USkeletalMeshSocket* socket = Cast<USkeletalMeshSocket>(USkeletalMeshSocket::StaticClass()->GetDefaultObject());
	socket->InitializeSocketFromLocation(Mesh, HitLocation, HitNormal);
	other->AttachRootComponentTo(Mesh, socket->BoneName, EAttachLocation::KeepWorldPosition);

	AProjectile* projectile = Cast<AFirstSprintProjectile>(other);
projectile->ProjectileMovement->UnregisterComponent();
projectile->CollisionComp->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}

Problem is I get weird result that the projectile is attached on the air, on the part where the capsule component is collide with the projectile.

What I need is the projectile’s attached in the mesh

Where do I went wrong? Do I need to change the collision component shape?

see pictures for references

Thanks!

EDIT
I use UnregisterComponent on ProjectileMovement component to stop the projectile from moving, and set the collision to NoCollision, since the projectile could still be moved and hit the enemy themselves if it’s not set to NoCollision

You don’t need to initialize a socket for that. I’ve disabled the collisions of projectiles on the capsule collider and set the character mesh to block them. After that just attach the component like this:

other->AttachRootComponentTo(Mesh, Hit.BoneName, EAttachLocation::KeepWorldPosition);

Hi xlar8or!

Unfortunately, the Hit.BoneName always have an empty result, that’s why I’m initializing a socket, anyway I’m using SnapToTarget to achieve my goal. It works for smaller enemy, but when facing a big enemy like picture above, the projectile also scaled into target size, which is kind of expected behavior from snapping but not like the result I’m hoping for

Hit.BoneName is empty because you are using the Capsule Collider to detect the hit, and that doesn’t have bones. You have to disable the collision with projectiles on the Capsule Collider and set it up on the Skeletal Mesh so it can fill the Hit.BoneName with the closest bone.

alt text !
Here are my collision settings for capsule collider and the skeletal mesh, when I set it like this, the character falls through the world.

Tried setting all into block but ignore the projectile on the capsule collider, but it still collides with the capsule collider
alt text

As you can see the capsule collider is blocking projectiles. Set the profile to Pawn and change it again to Custom and just change the projectile channel to ignore. Then make sure the mesh is blocking projectiles and the projectile blocks pawns and you should be good :slight_smile:

I set the correct collision and works gracefully now, thanks for your time!

You’re welcome :slight_smile: