碰撞类型有:
NoCollision:表面意思,没有碰撞
QueryOnly:能检测出碰撞,但是依然能够进行一个物体之间的重合overlay事件
PhysicsOnly:有碰撞,但不能检测出碰撞
QueryAndPhysics:有碰撞且有检测
设置自身碰撞类型,类型有:
下面的Hidden是只对C++开放,蓝图使用不了这些变量。
.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/BoxComponent.h"
#include "AFloorSwitch.generated.h"
UCLASS()
class MYCTEST_API AAFloorSwitch : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AAFloorSwitch();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
class UBoxComponent* TriggerBox;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
UStaticMeshComponent *FloorSwitch;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
UStaticMeshComponent* Door;
UFUNCTION()
virtual void NotifyActorBeginOverlap(AActor* OtherActor) override;
};
.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "AFloorSwitch.h"
#include "UObject/SparseDelegate.h"
#include "Engine/Engine.h"
#include "UObject/NoExportTypes.h"
#include "Components/PrimitiveComponent.h"
#include "Engine/World.h"
#include "TimerManager.h"
#include "Kismet/KismetStringLibrary.h"
#include "UObject/ConstructorHelpers.h"
#include "Components/StaticMeshComponent.h"
#include "Engine/StaticMesh.h"
// Sets default values
AAFloorSwitch::AAFloorSwitch()
{
// 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;
TriggerBox = CreateDefaultSubobject<UBoxComponent>(TEXT("Trigger"));
RootComponent = TriggerBox;
TriggerBox->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
TriggerBox->SetCollisionObjectType(ECollisionChannel::ECC_WorldStatic);
TriggerBox->SetCollisionResponseToAllChannels(ECR_Ignore);//把所有通道设置为忽略
TriggerBox->SetCollisionResponseToChannel(ECollisionChannel::ECC_Pawn, ECollisionResponse::ECR_Overlap);//单独把pawn通道设置为Overlay
TriggerBox->SetBoxExtent(FVector(64, 64, 32));//设置盒子大小
FloorSwitch = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("FloorSwitch"));
FloorSwitch->SetupAttachment(RootComponent);
}
// Called when the game starts or when spawned
void AAFloorSwitch::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AAFloorSwitch::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AAFloorSwitch::NotifyActorBeginOverlap(AActor* OtherActor)
{
Super::NotifyActorBeginOverlap(OtherActor);
FString theString = OtherActor->GetName();
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("Collision Enter! %s"), *theString));
}