ojas11

Untitled

May 6th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. One of my classes:
  2. // Fill out your copyright notice in the Description page of Project Settings.
  3.  
  4. #include "TankPlayerController.h"
  5.  
  6.  
  7.  
  8. ATank* ATankPlayerController::GetControlledTank() const {
  9. return Cast<ATank>(GetPawn());
  10. }
  11.  
  12. void ATankPlayerController::BeginPlay() {
  13. Super::BeginPlay();
  14. auto ControlledTank = GetControlledTank();
  15. if (!ControlledTank) {
  16. UE_LOG(LogTemp, Warning, TEXT("PlayerController not possesing a tank"));
  17. } else {
  18. UE_LOG(LogTemp, Warning, TEXT("PlayerController possessing: %s"), *ControlledTank->GetName());
  19. }
  20. }
  21.  
  22. void ATankPlayerController::Tick(float deltaTime) {
  23. Super::Tick(deltaTime);
  24. AimTowardsCrosshair();
  25. }
  26.  
  27. void ATankPlayerController::AimTowardsCrosshair() {
  28. if (!GetControlledTank()) { return; }
  29.  
  30. FVector HitLocation; // Out parameter
  31. if (GetSightRayHitLocation(HitLocation)) {
  32. GetControlledTank()->AimAt(HitLocation);
  33. }
  34. }
  35.  
  36. // Get world location if line-trace through cross-hair, true if it hits landscape
  37. bool ATankPlayerController::GetSightRayHitLocation(FVector &OutHitLocation) const {
  38. // Find the cross-hair position
  39. int32 ViewportSizeX, ViewportSizeY;
  40. GetViewportSize(ViewportSizeX, ViewportSizeY);
  41. auto ScreenLocation = FVector2D(ViewportSizeX * CrossHairXLocation, ViewportSizeY * CrossHairYLocation);
  42.  
  43. // "De-project" the screen position of the cross-hair to a world direction
  44. FVector LookDirection;
  45. if (GetLookDirection(ScreenLocation, LookDirection)) {
  46. GetLookVectorHitLocation(LookDirection, OutHitLocation);
  47. }
  48.  
  49. // Line-trace along that look direction, and see what we hit (up to max range)
  50. return true;
  51. }
  52.  
  53. bool ATankPlayerController::GetLookDirection(FVector2D ScreenLocation, FVector &LookDirection) const {
  54. FVector CameraWorldLocation; // To be discarded
  55. return DeprojectScreenPositionToWorld(ScreenLocation.X, ScreenLocation.Y, CameraWorldLocation, LookDirection);
  56. }
  57.  
  58. bool ATankPlayerController::GetLookVectorHitLocation(FVector LookDirection, FVector &HitLocation) const {
  59. FHitResult HitResult;
  60. auto StartLocation = PlayerCameraManager->GetCameraLocation();
  61. auto EndLocation = StartLocation + LookDirection * LineTraceRange;
  62. if (GetWorld()->LineTraceSingleByChannel(HitResult, StartLocation, EndLocation, ECollisionChannel::ECC_Visibility)) {
  63. // Set hit location
  64. HitLocation = HitResult.Location;
  65. return true;
  66. }
  67. HitLocation = FVector(0);
  68. return false; // Line trace didn't succeed
  69. }
  70. The .h file:
  71. // Fill out your copyright notice in the Description page of Project Settings.
  72.  
  73. #pragma once
  74.  
  75. #include "CoreMinimal.h"
  76. #include "Tank.h"
  77. #include "TankPlayerController.generated.h"
  78. /**
  79. *
  80. */
  81. UCLASS()
  82. class TANKTOOLS_API ATankPlayerController : public APlayerController
  83. {
  84. GENERATED_BODY()
  85.  
  86.  
  87. private:
  88.  
  89. ATank * GetControlledTank() const;
  90.  
  91. virtual void BeginPlay() override;
  92.  
  93. virtual void Tick(float deltaTime) override;
  94.  
  95. void AimTowardsCrosshair();
  96.  
  97. bool GetSightRayHitLocation(FVector &OutHitLocation) const;
  98.  
  99. UPROPERTY(EditAnywhere)
  100. float CrossHairXLocation = 0.5;
  101.  
  102. UPROPERTY(EditAnywhere)
  103. float CrossHairYLocation = 0.33333;
  104.  
  105. UPROPERTY(EditAnywhere)
  106. float LineTraceRange = 1000000;
  107.  
  108. bool GetLookDirection(FVector2D ScreenLocation, FVector &LookDirection) const;
  109.  
  110. bool GetLookVectorHitLocation(FVector LookDirection, FVector &HitLocation) const;
  111. };
Advertisement
Add Comment
Please, Sign In to add comment