Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- One of my classes:
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "TankPlayerController.h"
- ATank* ATankPlayerController::GetControlledTank() const {
- return Cast<ATank>(GetPawn());
- }
- void ATankPlayerController::BeginPlay() {
- Super::BeginPlay();
- auto ControlledTank = GetControlledTank();
- if (!ControlledTank) {
- UE_LOG(LogTemp, Warning, TEXT("PlayerController not possesing a tank"));
- } else {
- UE_LOG(LogTemp, Warning, TEXT("PlayerController possessing: %s"), *ControlledTank->GetName());
- }
- }
- void ATankPlayerController::Tick(float deltaTime) {
- Super::Tick(deltaTime);
- AimTowardsCrosshair();
- }
- void ATankPlayerController::AimTowardsCrosshair() {
- if (!GetControlledTank()) { return; }
- FVector HitLocation; // Out parameter
- if (GetSightRayHitLocation(HitLocation)) {
- GetControlledTank()->AimAt(HitLocation);
- }
- }
- // Get world location if line-trace through cross-hair, true if it hits landscape
- bool ATankPlayerController::GetSightRayHitLocation(FVector &OutHitLocation) const {
- // Find the cross-hair position
- int32 ViewportSizeX, ViewportSizeY;
- GetViewportSize(ViewportSizeX, ViewportSizeY);
- auto ScreenLocation = FVector2D(ViewportSizeX * CrossHairXLocation, ViewportSizeY * CrossHairYLocation);
- // "De-project" the screen position of the cross-hair to a world direction
- FVector LookDirection;
- if (GetLookDirection(ScreenLocation, LookDirection)) {
- GetLookVectorHitLocation(LookDirection, OutHitLocation);
- }
- // Line-trace along that look direction, and see what we hit (up to max range)
- return true;
- }
- bool ATankPlayerController::GetLookDirection(FVector2D ScreenLocation, FVector &LookDirection) const {
- FVector CameraWorldLocation; // To be discarded
- return DeprojectScreenPositionToWorld(ScreenLocation.X, ScreenLocation.Y, CameraWorldLocation, LookDirection);
- }
- bool ATankPlayerController::GetLookVectorHitLocation(FVector LookDirection, FVector &HitLocation) const {
- FHitResult HitResult;
- auto StartLocation = PlayerCameraManager->GetCameraLocation();
- auto EndLocation = StartLocation + LookDirection * LineTraceRange;
- if (GetWorld()->LineTraceSingleByChannel(HitResult, StartLocation, EndLocation, ECollisionChannel::ECC_Visibility)) {
- // Set hit location
- HitLocation = HitResult.Location;
- return true;
- }
- HitLocation = FVector(0);
- return false; // Line trace didn't succeed
- }
- The .h file:
- // Fill out your copyright notice in the Description page of Project Settings.
- #pragma once
- #include "CoreMinimal.h"
- #include "Tank.h"
- #include "TankPlayerController.generated.h"
- /**
- *
- */
- UCLASS()
- class TANKTOOLS_API ATankPlayerController : public APlayerController
- {
- GENERATED_BODY()
- private:
- ATank * GetControlledTank() const;
- virtual void BeginPlay() override;
- virtual void Tick(float deltaTime) override;
- void AimTowardsCrosshair();
- bool GetSightRayHitLocation(FVector &OutHitLocation) const;
- UPROPERTY(EditAnywhere)
- float CrossHairXLocation = 0.5;
- UPROPERTY(EditAnywhere)
- float CrossHairYLocation = 0.33333;
- UPROPERTY(EditAnywhere)
- float LineTraceRange = 1000000;
- bool GetLookDirection(FVector2D ScreenLocation, FVector &LookDirection) const;
- bool GetLookVectorHitLocation(FVector LookDirection, FVector &HitLocation) const;
- };
Advertisement
Add Comment
Please, Sign In to add comment