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.mzph.cn/news/597439.shtml

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

相关文章

Linux配置Acado

如果需要使用acado的matlab接口&#xff0c;请移步&#xff1a;Linux Matlab配置Acado 首先&#xff0c;安装必要的软件包&#xff1a; sudo apt-get install gcc g cmake git gnuplot doxygen graphviz在自定义目录下&#xff0c;下载源码 git clone https://github.com/ac…

windows+django+nginx部署静态资源文件

平台&#xff1a;windows python&#xff1a;3.10.0 django&#xff1a;4.0.8 nginx&#xff1a;1.24.0 背景 开发阶段采用前后端分离模式&#xff0c;现在要将项目部署到工控机上&#xff0c;把前端项目编译出来的静态文件放到后端项目中进行一体化部署&#xff0c;且不修改…

python打包exe

打包python绘制玫瑰花_python生成玫瑰花-CSDN博客 这个链接的程序 隐藏 控制台窗口&#xff08;如果你的程序是GUI&#xff0c;不是控制台应用可以选用&#xff0c;比如本案例的送你玫瑰花就是白底的&#xff09; 报错的话&#xff0c;可能没有pyinstaller这个库 参考&#x…

【KingbaseES】实现MySql函数Field

CREATE OR REPLACE FUNCTION field(value TEXT, VARIADIC arr TEXT[]) RETURNS INT AS $$ DECLAREi INT; BEGINFOR i IN 1 .. array_length(arr, 1) LOOPIF arr[i] value THENRETURN i;END IF;END LOOP;RETURN 0; END; $$ LANGUAGE plpgsql IMMUTABLE;

Apache的网页优化

掌握Apache网页压缩掌握Apache网页缓存掌握Apache隐藏版本信息掌握Apache网页防盗链 1.1 网页压缩 在使用 Apache 作为 Web 服务器的过程中&#xff0c;只有对 Apache 服务器进行适当的优化配 置&#xff0c;才能让 Apache 发挥出更好的性能。反过来说&#xff0c;如果 Apache…

项目初始化脚手架搭建

项目初始化脚手架搭建 仓库地址 easy-web: 一个快速初始化SpringBoot项目的脚手架 (gitee.com) 目前这个项目还是个单体项目&#xff0c;后续笔者有时间可能会改造成父子工程项目&#xff0c;将通用模块抽象出来&#xff0c;有兴趣的小伙伴也可以自行 CV 改造。 1、项目初始化…

【重点】【BFS】542.01矩阵

题目 法1&#xff1a;经典BFS 下图中就展示了我们方法&#xff1a; class Solution {public int[][] updateMatrix(int[][] mat) {int m mat.length, n mat[0].length;int[][] dist new int[m][n];boolean[][] used new boolean[m][n];Queue<int[]> queue new Li…

Spring Boot 3 集成 Thymeleaf

在现代的Web开发中&#xff0c;构建灵活、动态的用户界面是至关重要的。Spring Boot和Thymeleaf的结合为开发者提供了一种简单而强大的方式来创建动态的Web应用。本文将介绍如何在Spring Boot项目中集成Thymeleaf&#xff0c;并展示一些基本的使用方法。 什么是Thymeleaf&#…

未来已来,Ai原生应用与人高度结合!学习就在现在?

原生应用&#xff1a;OpenAI™ChatGPT、Baidu.Inc™文心一言 也可以体验CSDN的INSCODE AI&#xff0c;集成多个国内GPT内容。 文章目录 前言----编程语言的未来&#xff1f;一、编程语言的教育1.1 学校所见所闻1.2 开启我们的Ai行程~io&#xff01;1.3 Ai结果评论 二、Ai编程教…

Linux环境vscode clang-format格式化:vscode clang format command is not available

问题现象 vscode安装了clang-format插件&#xff0c;但是使用就报错 问题原因 设置中配置的clang-format插件工具路径不正确。 解决方案 确认本地安装了clang-format工具&#xff1a;终端输入clang-format&#xff08;也可能是clang-format-13等版本&#xff0c;建议tab自…

[NISACTF 2022]popchains

[NISACTF 2022]popchains wp 题目代码&#xff1a; Happy New Year~ MAKE A WISH <?phpecho Happy New Year~ MAKE A WISH<br>;if(isset($_GET[wish])){unserialize($_GET[wish]); } else{$anew Road_is_Long;highlight_file(__FILE__); } /**********************…

AI实景无人直播创业项目:开启自动直播新时代,一部手机即可实现增长

在当今社会&#xff0c;直播已经成为了人们日常生活中不可或缺的一部分。无论是商家推广产品、明星互动粉丝还是普通人分享生活&#xff0c;直播已经渗透到了各行各业。然而&#xff0c;传统直播方式存在着一些不足之处&#xff0c;如需现场主持人操作、高昂的费用等。近年来&a…

Android Studio 报错Failed to find Build Tools revision 28.0.3

目录 前言 一、报错信息 二、报错原因 三、解决方案 四、更多资源 前言 当Android Studio报错提示"Failed to find Build Tools revision 28.0.3"时&#xff0c;通常意味着您的项目需要使用28.0.3版本的构建工具&#xff0c;但系统中并没有找到对应的版本。这可…

西电期末1020.寻找同数

一.题目 二.分析与思路 其实就是寻找字串的个数&#xff0c;以前好像是有类似的题&#xff0c;先找到子串的首字符&#xff0c;再判断 三.代码实现 #include<bits/stdc.h>//万能头 bool f(char* s,char* sub,int i,int l){for(int j0;j<l;j){if(s[ji]!sub[j])retu…

计算机组成原理复习

一、计算机系统概论 计算机由硬件和软件两大部分组成 软件分系统软件和应用软件翻译程序有两种&#xff1a;编译程序和解释程序冯诺依曼计算机的特点&#xff1a; 计算机由运算器、存储器、控制器、输入设备和输出设备五大部分组成。指令和数据以同等地位存放于存储器内&#…

如何成为ChatGPT 优质Prompt创作者

如何提问&#xff1f; 我想让你成为我的Prompt创作者。你的目标是帮助我创作最佳的Prompt&#xff0c;这个Prompt将由你ChatGPT使用。你将遵循 以下过程&#xff1a;1.首先&#xff0c;你会问我Prompt是关于什么&#xff1f;我会告诉你&#xff0c;但我们需要 通过不断的重复来…

brew 安装使用 mysql、redis、mongodb

在 Mac 生态中 brew 真是个万能神器&#xff0c;今天就来介绍一下怎么使用 brew 安装 mysql、redis、mongodb&#xff0c;以及如何使用 brew 启动、关闭、重启这些服务。 前言 brew 常用命令 # 查看brew的版本 brew -v# 更新homebrew自己&#xff0c;把所有的Formula目录更新…

索引类型-哈希索引

一. 前言 前面我们简单介绍了数据库的B-Tree索引&#xff0c;下面我们介绍另一种索引类型-哈希索引。 二. 哈希索引的简介 哈希索引(hash index) 基于哈希表实现&#xff0c;只有精确匹配索引所有列的查询才有效。对于每一行数据&#xff0c;存储引擎都会对所有索引列计算一个…

华为 1+X《网络系统建设与运维(初级)》 认证实验上机模拟试题

华为 1X《网络系统建设与运维&#xff08;初级&#xff09;》认证实验上机模拟试题 一、考试背景二、考试说明2.1考试分数说明2.2考试要求2.3考试环境介绍2.4启动考试环境2.5保存答案 三、考试正文3.1考试内容3.1.1任务 1&#xff1a;设备连接3.1.2任务 2&#xff1a;设备命名3…

Centos7静态网络配置

在vmware中打开&#xff0c; 点击虚拟网络编辑器&#xff0c;修改以下配置 网关IP最后一位固定为2&#xff0c;这个160根据下图中vmnet8的ip地址来的 打开网络控制面板>打开vmnet8查看 接着打开linux&#xff0c;有桌面版的使用桌面版更加方便 箭头这么乱&#xff0c;但是你…