Hello All,
My problem is that I want to be able to spawn an actor with a UBoxComponent and use AddForce() on it. My items have a USkeletalMeshComponent, but I don’t know how to add a collision volume to a USkeletalMeshComponent. If anyone knows how I’d appreciate that info. But the point of this question is that if I can create an Actor with a USkeletalMeshComponent and a UBoxComponent with physics enabled on the box, and the box as the root component and the skeletal mesh attached to it, I figure I could just use AddForce on the box. This works when, instead of a box, I use a Capsule component. But when I switch out my capsule component for a box, it causes a crash when I start the game. Here’s the code that works (using a capsule component as the root component with physics enabled):
Header file:
UCLASS()
class MELEE_API AProjectile : public AActor
{
GENERATED_BODY()
public:
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Projectile)
USkeletalMeshComponent* Mesh;
UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Projectile)
UCapsuleComponent* Capsule;
// Sets default values for this actor's properties
AProjectile(const FObjectInitializer& ObjectInitializer);
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
};
and the source file:
AProjectile::AProjectile(const FObjectInitializer& ObjectInitializer) :
Super(ObjectInitializer.SetDefaultSubobjectClass<UMovementComponent>(TEXT("Movement")))
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Mesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
Capsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Capsule"));
RootComponent = Capsule;
Mesh->AttachTo(RootComponent);
Capsule->SetSimulatePhysics(true);
SetActorEnableCollision(true);
}
// Called when the game starts or when spawned
void AProjectile::BeginPlay()
{
Super::BeginPlay();
Capsule->AddForce(FVector(70000, -80000, 0), NAME_None, true);
Capsule->AddTorque(FVector(70000, -80000, 0), NAME_None, true);
}
// Called every frame
void AProjectile::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
and all I did to get the game to crash was go into the .h and the .cpp and replace the word UCapsuleComponent with UBoxComponent and the game crashes. By the way, in the editor, in the blueprint I select “BlockAllDynamic” for Collision presets. But it doesn’t seem to matter which preset I choose with the box. It always Crashes. Is there a way to get around this? I don’t want to have to use a capsule component each time I want to have an actor I wish to call AddForce() or AddTorque() on.