Guest User

TestPawn Main

a guest
Apr 10th, 2018
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "TestPawn.h"
  4.  
  5. // Sets default values
  6. ATestPawn::ATestPawn() {
  7. // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  8. PrimaryActorTick.bCanEverTick = true;
  9.  
  10. //Create our components
  11. RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
  12. OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
  13. OurCameraSpringArm->SetupAttachment(RootComponent);
  14. OurCameraSpringArm->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 20.0f), FRotator(-45.0f, 0.0f, 0.0f));
  15. OurCameraSpringArm->TargetArmLength = 400.f;
  16. OurCameraSpringArm->bEnableCameraLag = true;
  17. OurCameraSpringArm->CameraLagSpeed = 1000.0f;
  18.  
  19. OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));
  20. OurCamera->SetupAttachment(OurCameraSpringArm, USpringArmComponent::SocketName);
  21.  
  22. //Take control of the default Player
  23. AutoPossessPlayer = EAutoReceiveInput::Player0;
  24. }
  25.  
  26. // Called when the game starts or when spawned
  27. void ATestPawn::BeginPlay() {
  28. Super::BeginPlay();
  29.  
  30. //Take control of the default Player
  31. AutoPossessPlayer = EAutoReceiveInput::Player0;
  32. }
  33.  
  34. // Called every frame
  35. void ATestPawn::Tick(float DeltaTime) {
  36. Super::Tick(DeltaTime);
  37.  
  38. //Zoom in if ZoomIn button is down, zoom back out if it's not
  39. {
  40. if (bZoomingIn) {
  41. ZoomFactor += DeltaTime / 0.5f; //Zoom in over half a second
  42. }
  43. else {
  44. ZoomFactor -= DeltaTime / 0.25f; //Zoom out over a quarter of a second
  45. }
  46. ZoomFactor = FMath::Clamp<float>(ZoomFactor, 0.0f, 1.0f);
  47. //Blend our camera's FOV and our SpringArm's length based on ZoomFactor
  48. OurCamera->FieldOfView = FMath::Lerp<float>(90.0f, 60.0f, ZoomFactor);
  49. OurCameraSpringArm->TargetArmLength = FMath::Lerp<float>(400.0f, 300.0f, ZoomFactor);
  50. }
  51.  
  52. //Rotate our actor's yaw, which will turn our camera because we're attached to it
  53. {
  54. FRotator NewRotation = GetActorRotation();
  55. NewRotation.Yaw += CameraInput.X;
  56. SetActorRotation(NewRotation);
  57. }
  58.  
  59. //Rotate our camera's pitch, but limit it so we're always looking downward
  60. {
  61. FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
  62. NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);
  63. OurCameraSpringArm->SetWorldRotation(NewRotation);
  64. }
  65.  
  66. //Handle movement based on our "MoveX" and "MoveY" axes
  67. {
  68. if (!MovementInput.IsZero()) {
  69. //Scale our movement input axis values by 100 units per second
  70. MovementInput = MovementInput.GetSafeNormal() * 1000.0f;
  71. FVector NewLocation = GetNewLocation(GetActorLocation());
  72. SetActorLocation(NewLocation);
  73. }
  74. }
  75. }
  76.  
  77. FVector ATestPawn::GetNewLocation(FVector current) {
  78. FVector out = current;
  79. out += GetActorForwardVector() * MovementInput.X;
  80. out += GetActorRightVector() * MovementInput.Y;
  81. return out;
  82. }
  83.  
  84. // Called to bind functionality to input
  85. void ATestPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
  86. Super::SetupPlayerInputComponent(PlayerInputComponent);
  87.  
  88. //Hook up events for "ZoomIn"
  89. InputComponent->BindAction("ZoomIn", IE_Pressed, this, &ATestPawn::ZoomIn);
  90. InputComponent->BindAction("ZoomIn", IE_Released, this, &ATestPawn::ZoomOut);
  91.  
  92. //Hook up every-frame handling for our four axes
  93. InputComponent->BindAxis("P1MoveForward", this, &ATestPawn::MoveForward);
  94. InputComponent->BindAxis("P1MoveRight", this, &ATestPawn::MoveRight);
  95. InputComponent->BindAxis("CameraPitch", this, &ATestPawn::PitchCamera);
  96. InputComponent->BindAxis("CameraYaw", this, &ATestPawn::YawCamera);
  97. }
  98.  
  99. //Input functions
  100. void ATestPawn::MoveForward(float AxisValue) {
  101. MovementInput.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
  102. }
  103.  
  104. void ATestPawn::MoveRight(float AxisValue) {
  105. MovementInput.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
  106. }
  107.  
  108. void ATestPawn::PitchCamera(float AxisValue) {
  109. CameraInput.Y = AxisValue;
  110. }
  111.  
  112. void ATestPawn::YawCamera(float AxisValue) {
  113. CameraInput.X = AxisValue;
  114. }
  115.  
  116. void ATestPawn::ZoomIn() {
  117. bZoomingIn = true;
  118. }
  119.  
  120. void ATestPawn::ZoomOut() {
  121. bZoomingIn = false;
  122. }
Add Comment
Please, Sign In to add comment