Ue4

[UE4]マウスクリックの座標を取得します



Get Coordinates Mouse Click



PlayerControllerを使用して入手してください

1.現在のシーンの座標系でのマウスの位置を取得します。追加されたシーンマップの範囲は1000平方メートルであり、この位置の範囲も1000メートルx1000メートルです。



マウスイベントを登録する

FInputActionBinding &action1 = InputComponent->BindAction('SetDestination', IE_Pressed, this, &AHPlayerController::OnSetDestinationPressed)

この関数は、PlayerController :: PlayerTick()内で呼び出され、PlayerTick()をオーバーライドするMoveToMouseCursor()を実装します。



void AHPlayerController::MoveToMouseCursor() { // Trace to see what is under the mouse cursor FHitResult Hit GetHitResultUnderCursor(ECC_Visibility, false, Hit) if (Hit.bBlockingHit) { // We hit something, move there SetNewMoveDestination(Hit.ImpactPoint) } }

2.マウスディスプレイ内の座標系の位置を取得します。画面の解像度が1280x720の場合、位置の範囲は(0、0)から(1280、720)です。 PlayerController :: GetMousePosition()。

AHPlayerController* PC = ... float LocX = 0 float LocY = 0 PC->GetMousePosition(LocX, LocY)

3.シーン内のクリックの位置はタッチスクリーンデバイスで取得され、範囲は最初の場合と同じです。

タッチイベントの登録



InputComponent->BindTouch(EInputEvent::IE_Pressed, this, &AHPlayerController::MoveToTouchLocation)

関数の実装:

void AHPlayerController::MoveToTouchLocation(const ETouchIndex::Type FingerIndex, const FVector Location) { FVector2D ScreenSpaceLocation(Location) // Trace to see what is under the touch location FHitResult HitResult GetHitResultAtScreenPosition(ScreenSpaceLocation, CurrentClickTraceChannel, true, HitResult) if (HitResult.bBlockingHit) { // We hit something, move there SetNewMoveDestination(HitResult.ImpactPoint) } }

ビューポートインターフェイスを使用して取得します

/ / coordinate value is an integer FIntPoint MousePoint GEngine->GameViewport->Viewport->GetMousePos(MousePoint) //The coordinates are standard float FVector2D CursorPos GEngine->GameViewport->GetMousePosition(CursorPos)