UE5 C++(十七)— 射线检测

文章目录

前言

射线检测简单来说就是通过相机发射一条射线,用来检测对象的一种检测机制。

官网介绍:使用射线进行命中判定

这里主要介绍4种常用的射线检测方式。

根据通道进行射线检测

关键API:LineTraceSingleByChannel

声明变量

MyCharacter.h

cpp 复制代码
	//射线检测
	FVector StartLocation;
	FVector EndLocation;
	FVector Direction;
	FHitResult HitResult;

在Tick中实现通道进行射线检测

MyCharacter.cpp

cpp 复制代码
// Called every frame
void AMyCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	StartLocation = MyCamera->GetComponentLocation();
	Direction = MyCamera->GetForwardVector();
	EndLocation = StartLocation + Direction * 1000.f;
	// 根据通道进行射线检测
	bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, StartLocation, EndLocation, ECC_Visibility); // 根据通道查询检测
	if (bHit)
	{
		AActor *HitActor = HitResult.GetActor();
		FVector ImpactPoint = HitResult.ImpactPoint;
		FVector HitLocation = HitResult.Location;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("HitActor 1 :%s"), *HitActor->GetName()));
	}
}

编译 之后,在场景中添加CUBE运行

根据对象查询射线检测

关键API:LineTraceSingleByObjectType

声明变量

MyCharacter.h

cpp 复制代码
	//射线检测
	FVector StartLocation;
	FVector EndLocation;
	FVector Direction;
	FHitResult HitResult;

在Tick中实现通道进行射线检测

MyCharacter.cpp

cpp 复制代码
// Called every frame
void AMyCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	StartLocation = MyCamera->GetComponentLocation();
	Direction = MyCamera->GetForwardVector();
	EndLocation = StartLocation + Direction * 1000.f;
	// 根据对象查询检测
	//添加通道 按指定对象检测,非指定指定对象是不会检测到的
	FCollisionObjectQueryParams ObjectQueryParams;
	ObjectQueryParams.AddObjectTypesToQuery(ECC_WorldStatic);
	bool bHit2 = GetWorld()->LineTraceSingleByObjectType(HitResult, StartLocation, EndLocation, ObjectQueryParams); // 根据对象查询检测
	if(bHit2)
	{
		AActor *HitActor = HitResult.GetActor();
		FVector ImpactPoint = HitResult.ImpactPoint;
		FVector HitLocation = HitResult.Location;
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("HitActor 2 :%s"), *HitActor->GetName()));
	}
}

编译 之后,在场景中添加CUBE运行

多通道射线检测

关键API:LineTraceMultiByChannel

声明变量

MyCharacter.h

cpp 复制代码
	//射线检测
	FVector StartLocation;
	FVector EndLocation;
	FVector Direction;
	FHitResult HitResult;
	//多射线检测
	TArray<FHitResult> HitResults;

在Tick中实现通道进行射线检测

MyCharacter.cpp

cpp 复制代码
// Called every frame
void AMyCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	StartLocation = MyCamera->GetComponentLocation();
	Direction = MyCamera->GetForwardVector();
	EndLocation = StartLocation + Direction * 1000.f;
	// 根据多通道进行射线检测
		bool HitMuilt= GetWorld()->LineTraceMultiByChannel(HitResults, StartLocation, EndLocation, ECC_Visibility); // 多射线检测
	if (HitMuilt)
	{
		for (int32 i = 0; i < HitResults.Num(); i++)
		{
			AActor *HitActor = HitResults[i].GetActor();
			FVector ImpactPoint = HitResults[i].ImpactPoint;
			FVector HitLocation = HitResults[i].Location;
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("HitActor 3 :%s"), *HitActor->GetName()));
		}
	}
}

编译 之后,在场景中添加CUBE运行。

多射线对象查询检测

关键API:LineTraceMultiByObjectType

声明变量

MyCharacter.h

cpp 复制代码
	//射线检测
	FVector StartLocation;
	FVector EndLocation;
	FVector Direction;
	FHitResult HitResult;
	//多射线检测
	TArray<FHitResult> HitResults;

在Tick中实现通道进行射线检测

MyCharacter.cpp

cpp 复制代码
// Called every frame
void AMyCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	StartLocation = MyCamera->GetComponentLocation();
	Direction = MyCamera->GetForwardVector();
	EndLocation = StartLocation + Direction * 1000.f;
	// 多射线对象查询检测
	FCollisionObjectQueryParams ObjectQueryParams;
	ObjectQueryParams.AddObjectTypesToQuery(ECC_WorldStatic);
	ObjectQueryParams.AddObjectTypesToQuery(ECC_WorldDynamic);
	bool HitMuilt2 = GetWorld()->LineTraceMultiByObjectType(HitResults, StartLocation, EndLocation, ObjectQueryParams); // 多射线检测
	if (HitMuilt2)
	{
		for (int32 i = 0; i < HitResults.Num(); i++)
		{
			AActor *HitActor = HitResults[i].GetActor();
			FVector ImpactPoint = HitResults[i].ImpactPoint;
			FVector HitLocation = HitResults[i].Location;
			GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("HitActor 4 :%s"), *HitActor->GetName()));
		}
	}
}

编译之后,要把CUBE设置为ECC_WorldDynamic 类型碰撞

在场景中运行,也是能正常打印的。

相关推荐
-凌凌漆-40 分钟前
【Qt】QStringLiteral 介绍
开发语言·qt
程序员爱钓鱼40 分钟前
Go语言项目工程化 — 常见开发工具与 CI/CD 支持
开发语言·后端·golang·gin
小刘同学3211 小时前
C++11 特性
c++·c11新特性
军训猫猫头1 小时前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
真的想上岸啊1 小时前
学习C++、QT---18(C++ 记事本项目的stylesheet)
开发语言·c++·学习
吴梓穆2 小时前
UE5 重新编译插件版本
ue5
HECUgauss2 小时前
UE5 使用过程遇到的问题
ue5
m0_552200822 小时前
《UE5_C++多人TPS完整教程》学习笔记40 ——《P41 装备(武器)姿势(Equipped Pose)》
c++·游戏·ue5
明天好,会的2 小时前
跨平台ZeroMQ:在Rust中使用zmq库的完整指南
开发语言·后端·rust