国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

UE5 C++(十七)— 射線檢測

這篇具有很好參考價值的文章主要介紹了UE5 C++(十七)— 射線檢測。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前言

射線檢測簡單來說就是通過相機發(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運行
UE5 C++(十七)— 射線檢測,UE5 C++ 入門開發(fā),ue5,c++,開發(fā)語言
UE5 C++(十七)— 射線檢測,UE5 C++ 入門開發(fā),ue5,c++,開發(fā)語言

根據(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運行
UE5 C++(十七)— 射線檢測,UE5 C++ 入門開發(fā),ue5,c++,開發(fā)語言

多通道射線檢測

關(guān)鍵API:LineTraceMultiByChannel

聲明變量
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;
	// 根據(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 類型碰撞
UE5 C++(十七)— 射線檢測,UE5 C++ 入門開發(fā),ue5,c++,開發(fā)語言
在場景中運行,也是能正常打印的。文章來源地址http://www.zghlxwxcb.cn/news/detail-800417.html

到了這里,關(guān)于UE5 C++(十七)— 射線檢測的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Ue5 C++ metahuman

    參考官網(wǎng):?創(chuàng)建MetaHuman | Epic Developer Community (epicgames.com) 參考:?Quixel Bridge中的MetaHuman | Epic Developer Community (epicgames.com) 參考:導(dǎo)出到虛幻引擎5 | Epic Developer Community (epicgames.com) 參考:在Sequencer中使用MetaHuman | Epic Developer Community (epicgames.com) 待補充

    2024年02月10日
    瀏覽(46)
  • ue5 c++ interface 接口

    https://docs.unrealengine.com/5.2/en-US/interfaces-in-unreal-engine/ 1 純c++ 接口? 沒有ufunction 2 純c++ 有ufunction

    2024年02月10日
    瀏覽(50)
  • UE5 C++ UObject實例化

    UE5 C++ UObject實例化

    一.創(chuàng)建UObject C++類? 在MyObject中聲明結(jié)構(gòu)體FMyDataTableStruct 在MyPawn里面,先將頭文件里包含 MyObject.h 在MyPawn中聲明一個UMyObject類型的指針 TSubclassOf ?是提供 UClass 類型安全性的模板類。例如您在創(chuàng)建一個投射物類,允許設(shè)計者指定傷害類型。您可只創(chuàng)建一個 UClass 類型的 UPROPER

    2024年02月21日
    瀏覽(23)
  • UE5 C++(十四)— Interface的使用

    UE5 C++(十四)— Interface的使用

    接口是一系列抽象方法的聲明,是一些方法特征的集合,這些方法都應(yīng)該是抽象的,需要由具體的類去實現(xiàn),然后第三方就可以通過這組抽象方法調(diào)用,讓具體的類執(zhí)行具體的方法。 用c++實現(xiàn)接口類時需要注意一下幾點: 接口類中不可以聲明成員變量,靜態(tài)變量。 可以聲明

    2024年01月17日
    瀏覽(24)
  • Ue5.1創(chuàng)建C++項目(Rider)

    Ue5.1創(chuàng)建C++項目(Rider)

    1、下載地址 2、選擇左側(cè)“Visual Studio Community 2019” 3、選擇這兩個 4、單體里添加這幾個: 注意最后一個選下邊這個 5、下載完成后到這個目錄 將原來的14.29.30133文件夾重復(fù)名,改成14.29.30136,否則報錯 整整baidu、google了1天,網(wǎng)上都沒有解決辦法 Unable to find valid 14.29.30136 too

    2024年02月02日
    瀏覽(39)
  • UE5 c++ 的文件操作(記錄備忘)

    UE5 c++ 的文件操作(記錄備忘)

    函數(shù)庫.h 函數(shù)庫.cpp ? ? ?

    2024年02月14日
    瀏覽(22)
  • UE5 C++ UENUM 和 USTRUCT

    UE5 C++ UENUM 和 USTRUCT

    一.首先在APawn里聲明?UENUM 和 USTRUCT。UENUM 有兩種定義方式 一種是使用命名空間: 還有是繼承uint8: 通過申明class類 別名來替代 USTRUCT的定義?上面的第二種有類似但仍然有很多的差異: 首先要有GENERATED_USTRUCT_BODY()這個函數(shù) 并且參數(shù)要有 宏定義UPRPERTY 二.在?AMyPawn 里定義 En

    2024年02月22日
    瀏覽(18)
  • UE5創(chuàng)建C++項目里報錯

    UE5創(chuàng)建C++項目里報錯

    UE5創(chuàng)建C++項目里報錯: Running C:/Program Files/Epic Games/UE_5.0/Engine/Binaries/DotNET/UnrealBuildTool/UnrealBuildTool.exe? -projectfiles -project=\\\"F:/zhanting6.30/PAK/ReadWriteTxtProject/ReadWriteTxtProject.uproject\\\" -game -rocket -progress You must install or update .NET to run this application. App: C:Program FilesEpic GamesUE_5.0EngineBin

    2024年02月05日
    瀏覽(32)
  • UE5 C++ 靜態(tài)加載資源和類

    UE5 C++ 靜態(tài)加載資源和類

    一.上篇文章創(chuàng)建組件并綁定之后 在Actor中加載初始化了組件,現(xiàn)在在組件中賦值。使用static ConstructorHelpers::FObjectFinderTTempName(TEXT(\\\"Copy Reference\\\"));再用TempName.Object 里面的資源都來自StarterContent ? 效果如下: 二.靜態(tài)加載類 1.在Actor中再聲明一個AActor類? 2.在靜態(tài)加載類時使用

    2024年02月21日
    瀏覽(32)
  • UE5- c++ websocket客戶端寫法

    UE5- c++ websocket客戶端寫法

    ue5 c++ 實現(xiàn)socket客戶端,讀取服務(wù)端數(shù)據(jù),并進行解析 {projectName}.Build.cs里增加?\\\"WebSockets\\\",\\\"JsonUtilities\\\", \\\"Json\\\"配置信息,最終輸出如下: 說明,其中myue521是我的模塊名,文件名為myue521.Build.cs。 新增的模塊:\\\"WebSockets\\\",\\\"JsonUtilities\\\", \\\"Json\\\"。 準(zhǔn)備實現(xiàn)自己的webscoket_client。具體操作

    2024年02月10日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包