UE4学习-鼠标事件(按下、释放、物体抓取、计算重量、触发开门)
文章目錄
- 關鍵類
- 鼠標按下、釋放事件
- 物體抓取
- 計算物體重量
關鍵類
本篇博文用到的關鍵類有:
UInputComponent 用來綁定鼠標的按下和釋放事件 BindAction
UPhysicsHandleComponent 設置被抓取物體的抓取,移動,釋放
GrabComponentAtLocationWithRotation
ReleaseComponent
SetTargetLocation
UPrimitiveComponent 抓取組件抓取的對象
還可以獲取質量 GetMass
鼠標按下、釋放事件
首先,添加鼠標事件,在項目設置中,引擎->輸入->操作映射,按加號,添加一個映射對象,然后添加一個鼠標左鍵事件,一個空格鍵事件。
在代碼中創(chuàng)建一個 UInputComponent對象。
在grabber.h文件中,定義個人建議使用前置聲明,把頭文件放在cpp里面去包含。
并且添加兩個函數,一個表示按下,一個表示釋放。
#include <Components/InputComponent.h>void Grab();void Release();UInputComponent* inputComponent = nullptr;在cpp文件中添加:
// Called when the game starts void Ugrabber::BeginPlay() {Super::BeginPlay();inputComponent = GetOwner()->FindComponentByClass<UInputComponent>();if (nullptr != inputComponent){UE_LOG(LogTemp, Warning, TEXT("find inputComponent"));inputComponent->BindAction("grab", IE_Pressed, this, &Ugrabber::Grab);inputComponent->BindAction("grab", IE_Released, this, &Ugrabber::Release);}else{UE_LOG(LogTemp, Error, TEXT("not find inputComponent"));}}void Ugrabber::Grab() {UE_LOG(LogTemp, Warning, TEXT("inputComponent press")); }void Ugrabber::Release() {UE_LOG(LogTemp, Error, TEXT("inputComponent release")); }然后在虛幻編譯器中進行編譯,運行,鼠標按下釋放,空格按下釋放,都能打印日志,表示事件被觸發(fā)。
物體抓取
首先給pawn添加組件
編譯,保存。
添加的句柄對應的類是UPhysicsHandleComponent。
在grabber.h文件中,定義個人建議使用前置聲明,把頭文件放在cpp里面去包含。
并且添加兩個函數,一個表示按下,一個表示釋放。
class UPhysicsHandleComponent; UPhysicsHandleComponent* physicsHandel = nullptr;在grabber.cpp文件中添加:
BeginPlay() {physicsHandel = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>(); }// Called every frame void Ugrabber::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) {Super::TickComponent(DeltaTime, TickType, ThisTickFunction);// 設置抓取物體跟隨移動if (physicsHandel && physicsHandel->GrabbedComponent){physicsHandel->SetTargetLocation(getLineEnd());} }void Ugrabber::Grab() {UE_LOG(LogTemp, Warning, TEXT("inputComponent press"));FHitResult hit = lineTrace();UPrimitiveComponent* primit = hit.GetComponent();if (hit.GetActor() && physicsHandel){UE_LOG(LogTemp, Warning, TEXT("Grab"));physicsHandel->GrabComponentAtLocationWithRotation(primit, NAME_None, primit->GetOwner()->GetActorLocation(), GetOwner()->GetActorRotation());} }void Ugrabber::Release() {UE_LOG(LogTemp, Error, TEXT("inputComponent release"));if (physicsHandel){physicsHandel->ReleaseComponent();} }這里把物體的抓取和釋放的函數修改了。
計算物體重量
要計算物體重量,需要給物體添加一個選項: 生成重疊事件
還要給物體設置重量,桌子20kg,椅子10kg。
一定要把物體的 生成重疊事件 這個選項勾選,否則沒法被檢查到。物體質量也沒法計算。
在openDoor.h文件中添加一個函數:
float GetTotalMassInTrigger();在openDoor.cpp文件中添加如下代碼:
float UopenDoor::GetTotalMassInTrigger() {if (nullptr == trigger){return 0.0f;}TArray<AActor*> actArr;trigger->GetOverlappingActors(OUT actArr);float totalMass = 0.0f;for (const AActor* act : actArr){totalMass += act->FindComponentByClass<UPrimitiveComponent>()->GetMass();}UE_LOG(LogTemp, Warning, TEXT("total mass %f"), totalMass);return totalMass; }// Called every frame void UopenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) {Super::TickComponent(DeltaTime, TickType, ThisTickFunction);// if (nullptr != trigger && trigger->IsOverlappingActor(pawn))if(trigger && GetTotalMassInTrigger() > 25.0f){openDoor();lastTime = GetWorld()->GetTimeSeconds();}if (GetWorld()->GetTimeSeconds() - lastTime > openTime){closeDoor();} }在 TickComponent 函數中添加了質量判斷,還修改開門的方式,桌子和椅子一共的重量添加,才能觸發(fā)開門。
運行起來,桌子和椅子一起在觸發(fā)器范圍內,總質量為30kg,觸發(fā)機關,門打開了。
當然角色老是會在空中飄著,給角色也設置一個模擬物體的屬性,這樣物體就帶重力了,跳上去也會慢慢落下來。
到此,游戲基本完成,下篇博文會使用藍圖對開門等操作,設置動畫效果。
總結
以上是生活随笔為你收集整理的UE4学习-鼠标事件(按下、释放、物体抓取、计算重量、触发开门)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 公司对内担保和对外担保的区别 对内担保与
- 下一篇: 银行卡工资流水怎么查询 怎么查询银行卡的