前言
射線檢測簡單來說就是通過相機發(fā)射一條射線,用來檢測對象的一種檢測機制。
官網(wǎng)介紹:使用射線進行命中判定
這里主要介紹4種常用的射線檢測方式。
根據(jù)通道進行射線檢測
關(guān)鍵API:LineTraceSingleByChannel
聲明變量
MyCharacter.h
//射線檢測
FVector StartLocation;
FVector EndLocation;
FVector Direction;
FHitResult HitResult;
在Tick中實現(xiàn)通道進行射線檢測
MyCharacter.cpp
// Called every frame
void AMyCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
StartLocation = MyCamera->GetComponentLocation();
Direction = MyCamera->GetForwardVector();
EndLocation = StartLocation + Direction * 1000.f;
// 根據(jù)通道進行射線檢測
bool bHit = GetWorld()->LineTraceSingleByChannel(HitResult, StartLocation, EndLocation, ECC_Visibility); // 根據(jù)通道查詢檢測
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運行
根據(jù)對象查詢射線檢測
關(guān)鍵API:LineTraceSingleByObjectType
聲明變量
MyCharacter.h
//射線檢測
FVector StartLocation;
FVector EndLocation;
FVector Direction;
FHitResult HitResult;
在Tick中實現(xiàn)通道進行射線檢測
MyCharacter.cpp
// Called every frame
void AMyCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
StartLocation = MyCamera->GetComponentLocation();
Direction = MyCamera->GetForwardVector();
EndLocation = StartLocation + Direction * 1000.f;
// 根據(jù)對象查詢檢測
//添加通道 按指定對象檢測,非指定指定對象是不會檢測到的
FCollisionObjectQueryParams ObjectQueryParams;
ObjectQueryParams.AddObjectTypesToQuery(ECC_WorldStatic);
bool bHit2 = GetWorld()->LineTraceSingleByObjectType(HitResult, StartLocation, EndLocation, ObjectQueryParams); // 根據(jù)對象查詢檢測
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運行
多通道射線檢測
關(guān)鍵API:LineTraceMultiByChannel
聲明變量
MyCharacter.h
//射線檢測
FVector StartLocation;
FVector EndLocation;
FVector Direction;
FHitResult HitResult;
//多射線檢測
TArray<FHitResult> HitResults;
在Tick中實現(xiàn)通道進行射線檢測
MyCharacter.cpp文章來源:http://www.zghlxwxcb.cn/news/detail-800417.html
// Called every frame
void AMyCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
StartLocation = MyCamera->GetComponentLocation();
Direction = MyCamera->GetForwardVector();
EndLocation = StartLocation + Direction * 1000.f;
// 根據(jù)多通道進行射線檢測
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運行。
多射線對象查詢檢測
關(guān)鍵API:LineTraceMultiByObjectType
聲明變量
MyCharacter.h
//射線檢測
FVector StartLocation;
FVector EndLocation;
FVector Direction;
FHitResult HitResult;
//多射線檢測
TArray<FHitResult> HitResults;
在Tick中實現(xiàn)通道進行射線檢測
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設(shè)置為ECC_WorldDynamic 類型碰撞
在場景中運行,也是能正常打印的。文章來源地址http://www.zghlxwxcb.cn/news/detail-800417.html
到了這里,關(guān)于UE5 C++(十七)— 射線檢測的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!