1. 程式人生 > 實用技巧 >基於C++程式碼的UE4學習(二)—— 碰撞體

基於C++程式碼的UE4學習(二)—— 碰撞體

在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())方法來設定碰撞體的大小。

編譯後,碰撞體就生成了。

將該物件拖入關卡中,就可有看見一個碰撞體出現了: