在Unreal Engine中简单的碰撞体分为box,sphere和capsule。
今天以box碰撞体举例,其他的俩种构建方法都是相同的。
构建box碰撞体需要#include "Components\BoxComponent.h"头文件,记得导入
这是头文件,通过UBoxComponent类来定义box类型的碰撞体。
1 UCLASS()
2 class STANDARDDELEGATE_API ATriggerActor : public AActor
3 {
4 GENERATED_BODY()
5
6 public:
7 // Sets default values for this actor's properties
8 ATriggerActor();
9
10 protected:
11 // Called when the game starts or when spawned
12 virtual void BeginPlay() override;
13
14 public:
15 // Called every frame
16 virtual void Tick(float DeltaTime) override;
17
18 public:
19 UPROPERTY()
20 class UBoxComponent* box;
21 };
这是源文件
1 ATriggerActor::ATriggerActor()
2 {
3 // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
4 PrimaryActorTick.bCanEverTick = true;
5 box = CreateDefaultSubobject<UBoxComponent>(TEXT("box"));
6 box->InitBoxExtent(FVector(50.0f));
7 }
通过CreateDefaultSubobject方法来创建UBoxComponent类的对象。
并使用InitBoxExtent(FVector())方法来设置碰撞体的大小。
编译后,碰撞体就生成了。
将该对象拖入关卡中,就可有看见一个碰撞体出现了: