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

前言

射线检测简单来说就是通过相机发射一条射线,用来检测对象的一种检测机制。
官网介绍:使用射线进行命中判定

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

根据通道进行射线检测

关键API:LineTraceSingleByChannel

声明变量
MyCharacter.h

	//射线检测
	FVector StartLocation;
	FVector EndLocation;
	FVector Direction;
	FHitResult HitResult;

在Tick中实现通道进行射线检测
MyCharacter.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

	//射线检测
	FVector StartLocation;
	FVector EndLocation;
	FVector Direction;
	FHitResult HitResult;

在Tick中实现通道进行射线检测
MyCharacter.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

	//射线检测
	FVector StartLocation;
	FVector EndLocation;
	FVector Direction;
	FHitResult HitResult;
	//多射线检测
	TArray<FHitResult> HitResults;

在Tick中实现通道进行射线检测
MyCharacter.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

	//射线检测
	FVector StartLocation;
	FVector EndLocation;
	FVector Direction;
	FHitResult HitResult;
	//多射线检测
	TArray<FHitResult> HitResults;

在Tick中实现通道进行射线检测
MyCharacter.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 类型碰撞
在这里插入图片描述
在场景中运行,也是能正常打印的。

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-18 07:22:08       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-18 07:22:08       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-18 07:22:08       82 阅读
  4. Python语言-面向对象

    2024-01-18 07:22:08       91 阅读

热门阅读

  1. 微软推出了Copilot Pro 每月20美金

    2024-01-18 07:22:08       56 阅读
  2. AWS Secrets Manager 实战指南

    2024-01-18 07:22:08       59 阅读
  3. 敲诈勒索该怎么办

    2024-01-18 07:22:08       51 阅读
  4. 汽车售后服务客户满意度调查报告

    2024-01-18 07:22:08       52 阅读
  5. Spring boot 常见注解

    2024-01-18 07:22:08       50 阅读
  6. ChatGPT 和 文心一言 的优缺点及需求和使用场景

    2024-01-18 07:22:08       48 阅读
  7. GitLab服务器忘记root密码处理方式

    2024-01-18 07:22:08       52 阅读
  8. 【VUE】点击按钮下载全部链接文件

    2024-01-18 07:22:08       56 阅读
  9. SpringBoot-01

    2024-01-18 07:22:08       47 阅读