Just to preface, I’m very new to unreal so some of the terms may be used incorrectly and how things are supposed to be done in general.
What I’m attempting to do is basically have two actors of the same type I created (in this case two shopping carts that have wheels that roll them around and stuff), attach to each other whenever they bump into each other.
What I’m having issues with is when I run the code to attach them with a physics constraint, they go flying away really fast.
Here’s a cleaned up version of the code that’s attached to each carts tickcomponent script. I’m basically trying to have it so that, whenever a cart collides with another cart, it creates a physics constraint that attaches their bodies together. The If statement at the top is supposed to make sure it only creates one physics constraint and only runs when the collision is with another cart.
For a bit more clarity on what the carts are, they are blueprints with a body that is rectangular prism with physics enabled on top of four wheels that are attached to the body with physics constraints. The wheels roll with physics and carry the body around, basically like how a cart would in real life.
Apologies if the code isn’t very readable, I wasn’t sure of how the formatting on the forums worked exactly.
TArray<AActor*> Result;
GetOwner()->GetOverlappingActors(Result, USphereCartActor::StaticClass());
if (GEngine) {
for (int i = 0; i < Result.Num(); i++) {
if (Result[i]->GetClass() == GetOwner()->GetClass() && Result[i]->GetComponentsByTag(USceneComponent::StaticClass(), FName("AttachmentTag")).Num() == 0) {
>GetComponentsByTag(USceneComponent::StaticClass(), FName("BodyTag"))[0];
UActorComponent* other_body = Result[i]->GetComponentsByTag(USceneComponent::StaticClass(), FName("BodyTag"))[0];
USceneComponent* owner_body_scene = Cast<USceneComponent>(owner_body);
USceneComponent* other_body_scene = Cast<USceneComponent>(other_body);
UPhysicsConstraintComponent* NewPhysicsConstraint = NewObject<UPhysicsConstraintComponent>();
NewPhysicsConstraint->ComponentTags.Add(FName("AttachmentTag"));
NewPhysicsConstraint->SetVisibility(true);
NewPhysicsConstraint->SetConstrainedComponents(Cast<UPrimitiveComponent>(owner_body_scene), NAME_None, Cast<UPrimitiveComponent>(other_body_scene), NAME_None);
}
}
}