UE5 C++(十一)— 碰撞检测

文章目录

  • 代理绑定BeginOverlap和EndOverlap
  • Hit事件的代理绑定
  • 碰撞设置

代理绑定BeginOverlap和EndOverlap

首先,创建自定义ActorC++类 MyCustomActor
添加碰撞组件

#include "Components/BoxComponent.h"public:UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScene")class UBoxComponent *MyBoxComponent;
AMyCustomActor::AMyCustomActor()
{PrimaryActorTick.bCanEverTick = true;// 创建组件MySceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("CustomScene"));MyMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CustomStaticMesh"));MyBoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("CustomBox"));
}

动态绑定BeginOverlap和EndOverlap

public://声明绑定函数UFUNCTION()void BeginOverlapFunction(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);UFUNCTION()void EndOverlapFunction(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex);
// Called when the game starts or when spawned
void AMyCustomActor::BeginPlay()
{Super::BeginPlay();MyBoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AMyCustomActor::BeginOverlapFunction);MyBoxComponent->OnComponentEndOverlap.AddDynamic(this, &AMyCustomActor::EndOverlapFunction);
}void AMyCustomActor::BeginOverlapFunction(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("BeginOverlapFunction !!")));
}
void AMyCustomActor::EndOverlapFunction(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex)
{GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("EndOverlapFunction !!")));
}

编译脚本之后

在这里插入图片描述

创建刚才脚本的蓝图类 BP_MyCustomActor 并放到场景中

在这里插入图片描述
调整碰撞区域大小
在这里插入图片描述
然后,添加第三人称人物,并拖拽到场景中
在这里插入图片描述
在这里插入图片描述
运行之后,碰到和离开都会打印日志
在这里插入图片描述
完整的MyCustomActor脚本
MyCustomActor.h

// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
// 引入组件
#include "Components/SceneComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Components/BoxComponent.h"
#include "Components/AudioComponent.h"
#include "Particles/ParticleSystemComponent.h"
#include "GameFramework/Actor.h"
#include "MyCustomActor.generated.h"UCLASS()
class DEMO_API AMyCustomActor : public AActor
{GENERATED_BODY()public:// Sets default values for this actor's propertiesAMyCustomActor();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:// Called every framevirtual void Tick(float DeltaTime) override;// 自定义组件UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScene")class USceneComponent *MySceneComponent;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScene")class UStaticMeshComponent *MyMeshComponent;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScene")class UBoxComponent *MyBoxComponent;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScene")class UAudioComponent *MyAudioComponent;UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyScene")class UParticleSystemComponent *MyParticleSystemComponent;//声明绑定函数UFUNCTION()void BeginOverlapFunction(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult);UFUNCTION()void EndOverlapFunction(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex);
};

MyCustomActor.cpp


#include "MyCustomActor.h"// Sets default values
AMyCustomActor::AMyCustomActor()
{// 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;// 创建组件MySceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("CustomScene"));MyMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CustomStaticMesh"));MyBoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("CustomBox"));MyAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("CustomAudio"));MyParticleSystemComponent = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("CustomParticleSystem"));// 把组件添加到根组件RootComponent = MySceneComponent;MyMeshComponent->SetupAttachment(MySceneComponent);MyBoxComponent->SetupAttachment(MySceneComponent);MyAudioComponent->SetupAttachment(MyBoxComponent);MyParticleSystemComponent->SetupAttachment(MySceneComponent);
}// Called when the game starts or when spawned
void AMyCustomActor::BeginPlay()
{Super::BeginPlay();MyBoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AMyCustomActor::BeginOverlapFunction);MyBoxComponent->OnComponentEndOverlap.AddDynamic(this, &AMyCustomActor::EndOverlapFunction);
}// Called every frame
void AMyCustomActor::Tick(float DeltaTime)
{Super::Tick(DeltaTime);
}
void AMyCustomActor::BeginOverlapFunction(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("BeginOverlapFunction !!")));
}
void AMyCustomActor::EndOverlapFunction(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex)
{GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("EndOverlapFunction !!")));
}

Hit事件的代理绑定

以上面同样的方式创建Hit的绑定实现

UFUNCTION()void HitFunction(UPrimitiveComponent *HitComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, FVector NormalImpulse, const FHitResult &Hit);
void AMyCustomActor::BeginPlay()
{Super::BeginPlay();MyBoxComponent->OnComponentHit.AddDynamic(this, &AMyCustomActor::HitFunction);
}
void AMyCustomActor::HitFunction(UPrimitiveComponent *HitComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, FVector NormalImpulse, const FHitResult &Hit)
{GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("HitFunction !!")));
}

不同的是修改碰撞设置
这个是绑定BeginOverlap和EndOverlap
在这里插入图片描述

这个是Hit事件的代理绑定
在这里插入图片描述
Hit事件的代理绑定之后运行 ,当人物尝试一直前进碰到锥体时会一直触发事件
不像BeginOverlap和EndOverlap只会触发一次

在这里插入图片描述

碰撞设置

官网上有相关参考文档
为静态网格体设置碰撞体积
组件和碰撞
在这里插入图片描述
在C++脚本中设置

// Sets default values
AMyCustomActor::AMyCustomActor()
{// 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;// 创建组件MySceneComponent = CreateDefaultSubobject<USceneComponent>(TEXT("CustomScene"));MyMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("CustomStaticMesh"));MyBoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("CustomBox"));MyAudioComponent = CreateDefaultSubobject<UAudioComponent>(TEXT("CustomAudio"));MyParticleSystemComponent = CreateDefaultSubobject<UParticleSystemComponent>(TEXT("CustomParticleSystem"));// 把组件添加到根组件RootComponent = MySceneComponent;MyMeshComponent->SetupAttachment(MySceneComponent);MyBoxComponent->SetupAttachment(MySceneComponent);MyAudioComponent->SetupAttachment(MyBoxComponent);MyParticleSystemComponent->SetupAttachment(MySceneComponent);/****************************** 设置碰撞 ****************************************///碰撞设置MyBoxComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);MyBoxComponent->SetCollisionEnabled(ECollisionEnabled::QueryOnly);MyBoxComponent->SetCollisionEnabled(ECollisionEnabled::PhysicsOnly);MyBoxComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);MyBoxComponent->SetCollisionEnabled(ECollisionEnabled::ProbeOnly);MyBoxComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndProbe);//碰撞对象类型MyBoxComponent->SetCollisionObjectType(ECC_WorldStatic);MyBoxComponent->SetCollisionObjectType(ECC_WorldDynamic);MyBoxComponent->SetCollisionObjectType(ECC_Pawn);MyBoxComponent->SetCollisionObjectType(ECC_PhysicsBody);MyBoxComponent->SetCollisionObjectType(ECC_Vehicle);MyBoxComponent->SetCollisionObjectType(ECC_Destructible);//碰撞响应MyBoxComponent->SetCollisionResponseToAllChannels(ECR_Block);//对所有通道进行设置,响应为Block,阻挡MyBoxComponent->SetCollisionResponseToAllChannels(ECR_Overlap);//对所有通道进行设置,响应为Overlap,重叠MyBoxComponent->SetCollisionResponseToAllChannels(ECR_Ignore);//忽略MyBoxComponent->SetCollisionResponseToChannel(ECC_Pawn,ECR_Overlap);//对单个通道进行响应MyBoxComponent->SetCollisionResponseToChannel(ECC_WorldStatic,ECR_Block);//对单个通道进行响应MyBoxComponent->SetCollisionResponseToChannel(ECC_WorldDynamic,ECR_Ignore);//对单个通道进行响应}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/324374.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vmware安装centos 7.6 操作系统

vmware安装centos 7.6 操作系统 1、下载centos 7.6 操作系统镜像文件2、安装centos 7.6操作系统3、配置centos 7.6 操作系统3.1、配置静态IP地址 和 dns3.2、查看磁盘分区3.3、查看系统版本 1、下载centos 7.6 操作系统镜像文件 这里选择 2018年10月发布的 7.6 版本 官方下载链…

1月5日代码随想录完全二叉树的节点个数

222.完全二叉树的节点个数 给你一棵 完全二叉树 的根节点 root &#xff0c;求出该树的节点个数。 完全二叉树 的定义如下&#xff1a;在完全二叉树中&#xff0c;除了最底层节点可能没填满外&#xff0c;其余每层节点数都达到最大值&#xff0c;并且最下面一层的节点都集中在…

基于入侵杂草算法优化的Elman神经网络数据预测 - 附代码

基于入侵杂草算法优化的Elman神经网络数据预测 - 附代码 文章目录 基于入侵杂草算法优化的Elman神经网络数据预测 - 附代码1.Elman 神经网络结构2.Elman 神经用络学习过程3.电力负荷预测概述3.1 模型建立 4.基于入侵杂草优化的Elman网络5.测试结果6.参考文献7.Matlab代码 摘要&…

蟹目标检测数据集VOC格式400张

蟹&#xff0c;一种独特的海洋生物&#xff0c;以其强壮的身体和独特的生活习性而闻名。 蟹的身体宽厚&#xff0c;有一对锐利的大钳子&#xff0c;这使得它们在寻找食物和保护自己时非常有力。蟹的外观颜色多样&#xff0c;有绿色、蓝色、棕色和红色等&#xff0c;这使得它们在…

Go语言中的HTTP路由处理

在Web开发中&#xff0c;路由处理是至关重要的部分。它决定了当用户访问某个URL时&#xff0c;服务器应该如何响应。Go语言提供了多种库和工具来处理HTTP路由。下面&#xff0c;我们将深入了解如何在Go语言中处理HTTP路由。 Go语言的net/http包本身提供了基本的功能来处理路由…

【生成人工智能】Ray如何解决生成人工智能基础设施的常见生产挑战

这是我们生成人工智能博客系列的第一部分。在这篇文章中&#xff0c;我们讨论了如何使用Ray来生产常见的生成模型工作负载。即将发布的一篇博客将深入探讨Alpa等项目为什么要使用Ray来扩展大型模型。 生成的图像和语言模型有望改变企业的设计、支持、开发等方式。本博客重点关…

即时设计:轻松实现设计稿动画,打造独具魅力的GIF作品

制作动画 随着动画设计越来越受欢迎&#xff0c;设计师们需要一款强大的工具&#xff0c;以便轻松控制设计稿元素的属性&#xff0c;实现动画效果。今天&#xff0c;我们向您推荐一款具备帧动画功能的设计工具&#xff0c;它可以让您轻松调整元素的宽高、相对位置等属性&#x…

Visual studio 2010的安装与使用

一、下载及安装 1、下载软件。 百度网盘&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/115RibV7dOI_y8LUGW-94cA?pwd4hrs 提取码&#xff1a;4hrs 2、右键解压下载好的文件。 3、找到cn_visual_2010_……/Setup.hta&#xff0c;双击运行。 4、选择第三个“ Visual…

《Python自动化测试九章经》

Python是当前非常流行的一门编程语言&#xff0c;它除了在人工智能、数据处理、Web开发、网络爬虫等领域得到广泛使用之外&#xff0c;他也非常适合软件测试人员使用&#xff0c;但是&#xff0c;对于刚入行的测试小白来说&#xff0c;并不知道学习Python语言可以用来完成哪些测…

Linux的ping命令、wget命令、curl命令

一、ping命令 通过ping命令&#xff0c;可以检查指定的网络服务器是否是可联通状态 形式&#xff1a;ping [-c num] ip或主机名 -c&#xff1a;检查的次数&#xff0c;不使用-c&#xff0c;将无限次数持续检查 ip或主机名&#xff1a;被检查的服务器的ip地址或主机名地址 …

在mybatis中编写SQL时,提示表名和字段名的设置方法

一、在设置中搜索SQL Dialects 二、将Global SQL Dialect和Project SQL Dialect设置为MySQL 三、添加Path&#xff0c;选择当前项目&#xff0c;完成后应用 四、连接要使用的数据库&#xff0c;连接到具体要使用的数据库 五、连接完成后&#xff0c;在xml中编写sql语句就会有…

Linux第20步_在虚拟机上安装“Visual Studio Code”

1、双击windows系统桌面上的“FileZilla Client.exe”&#xff0c;打开FTP客户端&#xff0c;点击03软件下的Visual Studio Code&#xff0c;发现code_1.50.1-1602600906_amd64。 2、点击“文件”&#xff0c;然后点击“站点管理器”&#xff0c;见下图操作&#xff1a; 3、点…