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 <导入文件的路径名> …...
【Java学习笔记】Arrays类
Arrays 类 1. 导入包:import java.util.Arrays 2. 常用方法一览表 方法描述Arrays.toString()返回数组的字符串形式Arrays.sort()排序(自然排序和定制排序)Arrays.binarySearch()通过二分搜索法进行查找(前提:数组是…...
Debian系统简介
目录 Debian系统介绍 Debian版本介绍 Debian软件源介绍 软件包管理工具dpkg dpkg核心指令详解 安装软件包 卸载软件包 查询软件包状态 验证软件包完整性 手动处理依赖关系 dpkg vs apt Debian系统介绍 Debian 和 Ubuntu 都是基于 Debian内核 的 Linux 发行版ÿ…...
Java - Mysql数据类型对应
Mysql数据类型java数据类型备注整型INT/INTEGERint / java.lang.Integer–BIGINTlong/java.lang.Long–––浮点型FLOATfloat/java.lang.FloatDOUBLEdouble/java.lang.Double–DECIMAL/NUMERICjava.math.BigDecimal字符串型CHARjava.lang.String固定长度字符串VARCHARjava.lang…...
Ascend NPU上适配Step-Audio模型
1 概述 1.1 简述 Step-Audio 是业界首个集语音理解与生成控制一体化的产品级开源实时语音对话系统,支持多语言对话(如 中文,英文,日语),语音情感(如 开心,悲伤)&#x…...
蓝桥杯 冶炼金属
原题目链接 🔧 冶炼金属转换率推测题解 📜 原题描述 小蓝有一个神奇的炉子用于将普通金属 O O O 冶炼成为一种特殊金属 X X X。这个炉子有一个属性叫转换率 V V V,是一个正整数,表示每 V V V 个普通金属 O O O 可以冶炼出 …...
【Go语言基础【12】】指针:声明、取地址、解引用
文章目录 零、概述:指针 vs. 引用(类比其他语言)一、指针基础概念二、指针声明与初始化三、指针操作符1. &:取地址(拿到内存地址)2. *:解引用(拿到值) 四、空指针&am…...
排序算法总结(C++)
目录 一、稳定性二、排序算法选择、冒泡、插入排序归并排序随机快速排序堆排序基数排序计数排序 三、总结 一、稳定性 排序算法的稳定性是指:同样大小的样本 **(同样大小的数据)**在排序之后不会改变原始的相对次序。 稳定性对基础类型对象…...
免费数学几何作图web平台
光锐软件免费数学工具,maths,数学制图,数学作图,几何作图,几何,AR开发,AR教育,增强现实,软件公司,XR,MR,VR,虚拟仿真,虚拟现实,混合现实,教育科技产品,职业模拟培训,高保真VR场景,结构互动课件,元宇宙http://xaglare.c…...
AI语音助手的Python实现
引言 语音助手(如小爱同学、Siri)通过语音识别、自然语言处理(NLP)和语音合成技术,为用户提供直观、高效的交互体验。随着人工智能的普及,Python开发者可以利用开源库和AI模型,快速构建自定义语音助手。本文由浅入深,详细介绍如何使用Python开发AI语音助手,涵盖基础功…...
五子棋测试用例
一.项目背景 1.1 项目简介 传统棋类文化的推广 五子棋是一种古老的棋类游戏,有着深厚的文化底蕴。通过将五子棋制作成网页游戏,可以让更多的人了解和接触到这一传统棋类文化。无论是国内还是国外的玩家,都可以通过网页五子棋感受到东方棋类…...
