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);//对单个通道进行响应}
相关文章:

UE5 C++(十一)— 碰撞检测
文章目录 代理绑定BeginOverlap和EndOverlapHit事件的代理绑定碰撞设置 代理绑定BeginOverlap和EndOverlap 首先,创建自定义ActorC类 MyCustomActor 添加碰撞组件 #include "Components/BoxComponent.h"public:UPROPERTY(VisibleAnywhere, BlueprintRea…...
时序数据库InfluxDB、TimeScaleDB简介
一、时序数据库作用、优点 1、作用: 时序数据库通常被用在监控场景,比如运维和 IOT(物联网)领域。这类数据库旨在存储时序数据并实时处理它们。 比如。我们可以写一个程序将服务器上 CPU 的使用情况每隔 10 秒钟向 InfluxDB 中…...

复试 || 就业day05(2024.01.08)项目一
文章目录 前言代码模拟梯度下降构建函数与导函数函数的可视化求这个方程的最小值(直接求导)求方程最小值(不令方程导为0)【梯度下降】eta0.1eta 0.2eta 50eta 0.01画出eta0.1时的梯度下降x的变化过程 总结 前言 💫你…...

基于商品列表的拖拽排序后端实现
目录 一:实现思路 二:实现步骤 二:实现代码 三:注意点 一:实现思路 后台实现拖拽排序通常需要与前端进行配合,对商品的列表拖拽排序,前端需要告诉后端拖拽的元素和拖动的位置。 这里我们假…...

小游戏实战丨基于PyGame的贪吃蛇小游戏
文章目录 写在前面PyGame贪吃蛇注意事项系列文章写在后面 写在前面 本期内容:基于pygame的贪吃蛇小游戏 下载地址:https://download.csdn.net/download/m0_68111267/88700188 实验环境 python3.11及以上pycharmpygame 安装pygame的命令:…...

AOP(面向切面编程)基于XML方式配置
概念解释:(理解基本概念方可快速入手) 连接点(joinpoint) 被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法。 切入点(pointcut&#x…...

多线程的概念
多线程 同时执行多个任务,例如一个人一边听歌,一边跳舞 继承Thread类实现多线程的方式 定义一个MyThread类继承Thread类,重写里面的run方法 package com.itxs.demo01;/*** Classname : MyThread* Description : TODO 自定义线程继承Thread类*…...

DeepPurpose 生物化学深度学习库;蛋白靶点小分子药物对接亲和力预测虚拟筛选
参考: https://blog.csdn.net/c9Yv2cf9I06K2A9E/article/details/107649770 https://github.com/kexinhuang12345/DeepPurpose ##安装 pip install DeepPurpose rdkitDeepPurpose包括: 数据: 关联TDC库下载,是同一作者开发的 https://blog.csdn.net/weixin_42357472/artic…...
Java实现责任链模式
责任链模式是一种设计模式,用于处理请求的解耦。在责任链模式中,多个对象都有机会处理请求,从而避免了请求发送者和接收者之间的直接依赖关系。每个处理者都可以决定是否处理请求以及将请求传递给下一个处理者。 简介 责任链模式由一条链组…...

rabbitmq延时队列相关配置
确保 RabbitMQ 的延时消息插件已经安装和启用。你可以通过执行以下命令来安装该插件: rabbitmq-plugins enable rabbitmq_delayed_message_exchange 如果提示未安装,以下是安装流程: 查看mq版本: 查看自己使用的 MQ(…...

【工具】推荐一个好用的代码画图工具
PlantUML 官网地址:https://plantuml.com/zh/ 跳转 支持各种结构化数据画图支持代码调用jar包生成图片 提供在线画图能力 https://www.plantuml.com/plantuml/uml/SyfFKj2rKt3CoKnELR1Io4ZDoSa70000 有兴趣可以尝试下 over~~...
Leetcode14-判断句子是否为全字母句(1832)
1、题目 全字母句 指包含英语字母表中每个字母至少一次的句子。 给你一个仅由小写英文字母组成的字符串 sentence ,请你判断 sentence 是否为 全字母句 。 如果是,返回 true ;否则,返回 false 。 示例 1: 输入&am…...

HTTP和TCP代理原理及实现,主要是理解
Web 代理是一种存在于网络中间的实体,提供各式各样的功能。现代网络系统中,Web 代理无处不在。我之前有关 HTTP 的博文中,多次提到了代理对 HTTP 请求及响应的影响。今天这篇文章,我打算谈谈 HTTP 代理本身的一些原理,…...
MySQL中的连接池
数据库的连接池 1 )概述 网站连接数据库,为庞大用户的每次请求创建一个连接是不合适的关闭并重新连接的成本是很大的处理方法:设置最大值, 最小值, 设置最多闲置连接,设置等待阻塞 2 )示例演示 import threading i…...
css计时器 animation实现计时器延时器
css计时器 animation实现计时器延时器 缺点当切页面导航会休眠不执行 最初需求是一个列表每个项目都有各自的失效时间 然后就想到 计时器延时器轮询等方案 这些方案每一个都要有自己的计时器 感觉不是很好 轮询也占资源 然后突发奇想 css能不能实现 开始想到的是transition测…...

【win11 绕过TPM CPU硬件限制安装】
Qt编程指南 VX:hao541022348 ■ 下载iso文件■ 右键文件点击装载出现如下问题■ 绕过TPM CPU硬件限制安装方法■ 虚拟机安装win11 ■ 下载iso文件 选择Windows11 (multi-edition ISO)在选择中文 ■ 右键文件点击装载出现如下问题 ■ 绕过T…...
k8s的yaml文件中的kind类型都有哪些?(清单版本)
在操作kubernetes的过程中,我们接触到的yaml文件中的kind类型有很多。他们代表了kubernetes的不同类型的对象,了解了kind的类型,也就相当于了解了k8s都有哪些类型的对象。 类型清单及概要说明 序号类型简述1Pod一个Kubernetes中最基本的资源…...
Jetpack Room使用
Room使用 回顾 数据库有多张表,一张表只能记录一种Class,Class的具体属性是这个表的列;所有对表的操作都要通过Dao来访问 注解说明: Enity 作用于Class上,表示创建一张表记录该Class,Class内部属性使用…...

HarmonyOS应用开发之ArkTS语言学习记录
1、ArkTS介绍 ArkTS是鸿蒙生态的应用开发语言。它在保持TypeScript(简称TS)基本语法风格的基础上,对TS的动态类型特性施加更严格的约束,引入静态类型。同时,提供了声明式UI、状态管理等相应的能力,让开发者…...
windows 下 mongodb6.0 导入导出json文件
1.运行cmd窗口,进入MongoDB安装路径下的bin文件下,输入以下命令导入数据文件 mongoimport --host 127.0.0.1 --port 27017 --db <数据库名称,根据自个情况> -c <集合名称,自定义> --file <导入文件的路径名> …...
Python爬虫实战:研究MechanicalSoup库相关技术
一、MechanicalSoup 库概述 1.1 库简介 MechanicalSoup 是一个 Python 库,专为自动化交互网站而设计。它结合了 requests 的 HTTP 请求能力和 BeautifulSoup 的 HTML 解析能力,提供了直观的 API,让我们可以像人类用户一样浏览网页、填写表单和提交请求。 1.2 主要功能特点…...
Spring Boot面试题精选汇总
🤟致敬读者 🟩感谢阅读🟦笑口常开🟪生日快乐⬛早点睡觉 📘博主相关 🟧博主信息🟨博客首页🟫专栏推荐🟥活动信息 文章目录 Spring Boot面试题精选汇总⚙️ **一、核心概…...

保姆级教程:在无网络无显卡的Windows电脑的vscode本地部署deepseek
文章目录 1 前言2 部署流程2.1 准备工作2.2 Ollama2.2.1 使用有网络的电脑下载Ollama2.2.2 安装Ollama(有网络的电脑)2.2.3 安装Ollama(无网络的电脑)2.2.4 安装验证2.2.5 修改大模型安装位置2.2.6 下载Deepseek模型 2.3 将deepse…...
Java编程之桥接模式
定义 桥接模式(Bridge Pattern)属于结构型设计模式,它的核心意图是将抽象部分与实现部分分离,使它们可以独立地变化。这种模式通过组合关系来替代继承关系,从而降低了抽象和实现这两个可变维度之间的耦合度。 用例子…...

免费PDF转图片工具
免费PDF转图片工具 一款简单易用的PDF转图片工具,可以将PDF文件快速转换为高质量PNG图片。无需安装复杂的软件,也不需要在线上传文件,保护您的隐私。 工具截图 主要特点 🚀 快速转换:本地转换,无需等待上…...

消息队列系统设计与实践全解析
文章目录 🚀 消息队列系统设计与实践全解析🔍 一、消息队列选型1.1 业务场景匹配矩阵1.2 吞吐量/延迟/可靠性权衡💡 权衡决策框架 1.3 运维复杂度评估🔧 运维成本降低策略 🏗️ 二、典型架构设计2.1 分布式事务最终一致…...

GraphQL 实战篇:Apollo Client 配置与缓存
GraphQL 实战篇:Apollo Client 配置与缓存 上一篇:GraphQL 入门篇:基础查询语法 依旧和上一篇的笔记一样,主实操,没啥过多的细节讲解,代码具体在: https://github.com/GoldenaArcher/graphql…...

UE5 音效系统
一.音效管理 音乐一般都是WAV,创建一个背景音乐类SoudClass,一个音效类SoundClass。所有的音乐都分为这两个类。再创建一个总音乐类,将上述两个作为它的子类。 接着我们创建一个音乐混合类SoundMix,将上述三个类翻入其中,通过它管理每个音乐…...
信息系统分析与设计复习
2024试卷 单选题(20) 1、在一个聊天系统(类似ChatGPT)中,属于控制类的是()。 A. 话语者类 B.聊天文字输入界面类 C. 聊天主题辨别类 D. 聊天历史类 解析 B-C-E备选架构中分析类分为边界类、控制类和实体类。 边界…...

可下载旧版app屏蔽更新的app市场
软件介绍 手机用久了,app越来越臃肿,老手机卡顿成常态。这里给大家推荐个改善老手机使用体验的方法,还能帮我们卸载不需要的app。 手机现状 如今的app不断更新,看似在优化,实则内存占用越来越大,对手机性…...