Attach collision component to mesh component?

Hi, I am trying to figure out something I’m sure will end up being simple, but I’m new to the whole UE4 programming scene. I have a box mesh in my assets created from a brush. I know how to assign a mesh component in c++ and make it the root component, I also know how to create a box collider component. The issue I’m having, is attaching the box collider to the mesh component where as it will always keep the same location and rotation as the mesh comp (root comp). I’ve tried using AttachTo and AttachParent to no avail. I know I can just assign 6DOP Simplified collision in the Static Mesh Editor, but what’s the fun in that? I’d like to do as much via c++ as possible for bragging rights :wink:
Any help is greatly appreciated, thanks in advance!

I got it working using AttachParent so theoretically what you did shall work but to help you practically the source code of your constructor will be helpful.

Hi, thanks for the reply. I tried to do some homework on how to use AttachParent and just cannot find a solid example. AttachParent sounds like a function to me so at first I was doing AttachParent() and trying to call params lol. Then I removed the parentheses and things compiled, still nothing happened. So I thought maybe I should think of it as AttachedParent, So I tried AttachParent = MeshComp, AttachParent = RootComponent. Now the collider isn’t showing up in the editor at all, when at first was just showing up in the middle of the world but not being parented. I tried BoxComp->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); after parenting as well. I’ve done so many searches but the search here doesn’t point me toward the api reference or anything, it’s really difficult for me to find an example. Here’s what I have tried so far.

APhys::APhys(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	static ConstructorHelpers::FObjectFinder<UStaticMesh>Mesh(TEXT("StaticMesh'/Game/Meshes/Box.Box'"));
	MeshComp = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("Mesh"));
	BoxComp = PCIP.CreateDefaultSubobject<UBoxComponent>(this, TEXT("BoxCollider"));
	MeshComp->StaticMesh = Mesh.Object;
	RootComponent = MeshComp;
	MeshComp->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
	BoxComp->AttachParent = RootComponent;
	BoxComp->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
	PrimaryActorTick.bCanEverTick = true;
}

Update: AttachParent was working after all, I made the bounds scale larger so I could see it better in the editor, and used AttachParent = MeshComp. It’s showing up and staying w/ the mesh, but no collision is happening. I tried using BoxComp->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics)…

AttachParent was working after all. Here are the two lines I needed to get the collision to actually work with other actors.

BoxComp->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
BoxComp->SetCollisionEnabled(ECollisionEnabled::Type::QueryAndPhysics);

thanks, I’ve been searching for this for 3 hours now, I can’t belive it’s that hard to find.