Advertisement
N0B0DY1x

PlayerCharacter.cpp

Dec 1st, 2020
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3.  
  4. #include "PlayerCharacter.h"
  5. #include "GameFramework/CharacterMovementComponent.h"
  6. #include "GameFramework/SpringArmComponent.h"
  7.  
  8. // Sets default values
  9. APlayerCharacter::APlayerCharacter()
  10. {
  11.     // Configure character movements
  12.     GetCharacterMovement()->bOrientRotationToMovement = true;
  13.     GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f);
  14.     GetCharacterMovement()->JumpZVelocity = 600.0f;
  15.     GetCharacterMovement()->AirControl = 0.75f;
  16.    
  17.     bUseControllerRotationPitch = false;
  18.  
  19.     // FPS Camera
  20.     FPSCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
  21.     FPSCamera->SetupAttachment(RootComponent);
  22.     FPSCamera->SetRelativeLocation(FVector(30.0f, 0.0f, BaseEyeHeight));
  23.     FPSCamera->bUsePawnControlRotation = true;
  24.     FPSCamera->bAutoActivate = false;
  25.    
  26.     // Create a camera boom (pulls in towards the player if there is a collision)
  27.     CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
  28.     CameraBoom->SetupAttachment(RootComponent);
  29.     CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character  
  30.     CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
  31.    
  32.     // Create a follow camera
  33.     TPSCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
  34.     TPSCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
  35.     // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
  36.     TPSCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
  37.  
  38.     // Base configuration for camera view
  39.     bThirdPerson = true;
  40.     bUseControllerRotationYaw = false;
  41.     bUseControllerRotationRoll = false;
  42. }
  43.  
  44. // Called when the game starts or when spawned
  45. void APlayerCharacter::BeginPlay()
  46. {
  47.     Super::BeginPlay();
  48. }
  49.  
  50. // Called to bind functionality to input
  51. void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
  52. {
  53.     Super::SetupPlayerInputComponent(PlayerInputComponent);
  54.  
  55.     // Movements bindings
  56.     PlayerInputComponent->BindAxis("MoveForward", this, &APlayerCharacter::MoveForward);
  57.     PlayerInputComponent->BindAxis("MoveRight", this, &APlayerCharacter::MoveRight);
  58.  
  59.     PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
  60.     PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  61.  
  62.     // Look around bindings
  63.     PlayerInputComponent->BindAxis("Turn", this, &APlayerCharacter::AddControllerYawInput);
  64.     PlayerInputComponent->BindAxis("LookUp", this, &APlayerCharacter::AddControllerPitchInput);
  65.  
  66.     // Change player view
  67.     PlayerInputComponent->BindAction("ChangeView", IE_Pressed, this, &APlayerCharacter::ChangeView);
  68. }
  69.  
  70. void APlayerCharacter::MoveForward(const float Value)
  71. {
  72.     if (Controller != nullptr && Value != 0.0f)
  73.     {
  74.         // find out which way is forward
  75.         const FRotator Rotation = Controller->GetControlRotation();
  76.         const FRotator YawRotation(0, Rotation.Yaw, 0);
  77.  
  78.         // get forward vector
  79.         const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
  80.         AddMovementInput(Direction, Value);
  81.     }
  82. }
  83.  
  84. void APlayerCharacter::MoveRight(const float Value)
  85. {
  86.     if (Controller != nullptr && Value != 0.0f)
  87.     {
  88.         // find out which way is right
  89.         const FRotator Rotation = Controller->GetControlRotation();
  90.         const FRotator YawRotation(0, Rotation.Yaw, 0);
  91.  
  92.         // get right vector
  93.         const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
  94.         // add movement in that direction
  95.         AddMovementInput(Direction, Value);
  96.     }
  97. }
  98.  
  99. void APlayerCharacter::StartJump()
  100. {
  101.     bPressedJump = true;
  102. }
  103.  
  104. void APlayerCharacter::StopJump()
  105. {
  106.     bPressedJump = false;
  107. }
  108.  
  109. // Switch from TPS to FPS
  110. void APlayerCharacter::ChangeView()
  111. {
  112.     bThirdPerson = !bThirdPerson;
  113.    
  114.     bUseControllerRotationYaw = !bThirdPerson;
  115.     bUseControllerRotationRoll = !bThirdPerson;
  116.     GetCharacterMovement()->bOrientRotationToMovement = bThirdPerson;
  117.    
  118.     if (bThirdPerson)
  119.     {
  120.         FPSCamera->Deactivate();
  121.         TPSCamera->Activate();
  122.     }
  123.     else
  124.     {
  125.         TPSCamera->Deactivate();
  126.         FPSCamera->Activate();
  127.     }
  128.  
  129.     // const FString SText = FString::Printf(TEXT("fps replicated : %d, tps replicated %d"), FPSCamera->GetIsReplicated(), TPSCamera->GetIsReplicated());
  130.     // GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, SText);
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement