[UE4] [C ++]ブループリントにUIを追加し(水平...)、配置の方向を取得します(回転...)



Add Ui Blueprint Horizontal



ブループリントはUIを追加します:

1.UIを作成します




二、



画像を追加

3.対応する画像を追加し、照準点の位置と照準位置を調整します(画像の位置に対して)



位置(白❀を直接30,50に移動)

照準位置(0.5,0.5の設定で画像の中央を狙うことができます)

4.PlayerControllerにUIを追加します


ウィジェットの作成(最初にUIを作成)--->ビューポートに追加(画面に追加)


十字線の位置合わせの方向を取得します:(C ++)

提供される方法:

GetViewPortSize(int32&SizeX、int32&SizeY)

//これらの2つの値(SizeX、SizeY)には値が割り当てられます:画面のXY(PlayerControllerのメソッド)

//十字線の位置(SizeX * Xに対応する配置(青写真で設定)、SizeY * Yに対応する配置)


DeprojectScreenPositionToWorld(float ScreenX、float ScreenY、FVector&WorldLocation、FVector&WorldDirection)

//画面上の十字線の位置によって(十字線のWorldLocationの世界座標と照準方向WorldDirectionを返します)


PlayerCameraManager-> GetCameraLocation()

// PlayerControllerの下のメソッドで、カメラの位置をStartLocation(光線の開始点)として取得します


GetWorld()-> LineTraceSingelByChannel(//ブール値を返します(ヒットするとtrue)

struct FHitResult&OutHit、//発信光線が当たったターゲットを返します

const FVector&Start、//光線の開始

const FVector&End、//放射終了

ECollisionChannel TraceChannel、//どのタイプのターゲットを撃つかを設定します(ECC_Visibility)

const FCollisionQueryParams&Params / * = FCollisionQueryParams :: DefaultQueryParam * /、//デフォルト値があります

const FCollisionResponseParams&ResponseParam / * = FCollisionResponseParams :: DefaultResponseParam * //デフォルト値があります

)。


void ATankPlayerController::Tick(float DeltaTime) { //Move to AimLocation Move the line of sight AimToTarget()//Keep looking for which point we want to hit } void ATankPlayerController::AimToTarget() { FVector HitLocation if (GetSightRayLocation(HitLocation)) { //Move the turret } } bool ATankPlayerController::GetSightRayLocation(FVector &OutHitLocation) { int32 ViewportSizeX, ViewportSizeY GetViewportSize(ViewportSizeX, ViewportSizeY)//These two values ​​are given the size of the screen (method in PlayerController) FVector2D ScreenLocation = FVector2D(ViewportSizeX*CrosshairXLocation, ViewportSizeY*CrosshairYLocation)//The position of the crosshair on the screen FVector WorldLocation FVector WorldDirection //LookDirection if (DeprojectScreenPositionToWorld(ScreenLocation.X, ScreenLocation.Y, WorldLocation, WorldDirection)) {//return a bool value to determine if it is found GetLookVectorHitLocation(WorldDirection, OutHitLocation) return true } return false } //Return a ray impact point bool ATankPlayerController::GetLookVectorHitLocation(FVector LookLocation, FVector & OutHitLocation) { //Create ray (start point, end point) FVector StartLocation = PlayerCameraManager->GetCameraLocation()//Method under PlayerController (Get the location of the camera) FVector EndLocation = StartLocation + LookLocation * LineTraceRange FHitResult HitResult if (GetWorld()->LineTraceSingleByChannel( HitResult, StartLocation, EndLocation, ECollisionChannel::ECC_Visibility )) { OutHitLocation = HitResult.Location//The specific impact point location is transmitted return true } OutHitLocation = FVector(0.0f)//If not found, give a default value to the outgoing value return true }