U++ 如何实现hit, overlap等事件 以及U++基本碰撞类型

      

碰撞类型有:

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));
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值