Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "PlayerCharacter.h"
- #include "GameFramework/CharacterMovementComponent.h"
- #include "GameFramework/SpringArmComponent.h"
- // Sets default values
- APlayerCharacter::APlayerCharacter()
- {
- // Configure character movements
- GetCharacterMovement()->bOrientRotationToMovement = true;
- GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f);
- GetCharacterMovement()->JumpZVelocity = 600.0f;
- GetCharacterMovement()->AirControl = 0.75f;
- bUseControllerRotationPitch = false;
- // FPS Camera
- FPSCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
- FPSCamera->SetupAttachment(RootComponent);
- FPSCamera->SetRelativeLocation(FVector(30.0f, 0.0f, BaseEyeHeight));
- FPSCamera->bUsePawnControlRotation = true;
- FPSCamera->bAutoActivate = false;
- // Create a camera boom (pulls in towards the player if there is a collision)
- CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
- CameraBoom->SetupAttachment(RootComponent);
- CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character
- CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
- // Create a follow camera
- TPSCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
- TPSCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
- // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
- TPSCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
- // Base configuration for camera view
- bThirdPerson = true;
- bUseControllerRotationYaw = false;
- bUseControllerRotationRoll = false;
- }
- // Called when the game starts or when spawned
- void APlayerCharacter::BeginPlay()
- {
- Super::BeginPlay();
- }
- // Called to bind functionality to input
- void APlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
- {
- Super::SetupPlayerInputComponent(PlayerInputComponent);
- // Movements bindings
- PlayerInputComponent->BindAxis("MoveForward", this, &APlayerCharacter::MoveForward);
- PlayerInputComponent->BindAxis("MoveRight", this, &APlayerCharacter::MoveRight);
- PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
- PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
- // Look around bindings
- PlayerInputComponent->BindAxis("Turn", this, &APlayerCharacter::AddControllerYawInput);
- PlayerInputComponent->BindAxis("LookUp", this, &APlayerCharacter::AddControllerPitchInput);
- // Change player view
- PlayerInputComponent->BindAction("ChangeView", IE_Pressed, this, &APlayerCharacter::ChangeView);
- }
- void APlayerCharacter::MoveForward(const float Value)
- {
- if (Controller != nullptr && Value != 0.0f)
- {
- // find out which way is forward
- const FRotator Rotation = Controller->GetControlRotation();
- const FRotator YawRotation(0, Rotation.Yaw, 0);
- // get forward vector
- const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
- AddMovementInput(Direction, Value);
- }
- }
- void APlayerCharacter::MoveRight(const float Value)
- {
- if (Controller != nullptr && Value != 0.0f)
- {
- // find out which way is right
- const FRotator Rotation = Controller->GetControlRotation();
- const FRotator YawRotation(0, Rotation.Yaw, 0);
- // get right vector
- const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
- // add movement in that direction
- AddMovementInput(Direction, Value);
- }
- }
- void APlayerCharacter::StartJump()
- {
- bPressedJump = true;
- }
- void APlayerCharacter::StopJump()
- {
- bPressedJump = false;
- }
- // Switch from TPS to FPS
- void APlayerCharacter::ChangeView()
- {
- bThirdPerson = !bThirdPerson;
- bUseControllerRotationYaw = !bThirdPerson;
- bUseControllerRotationRoll = !bThirdPerson;
- GetCharacterMovement()->bOrientRotationToMovement = bThirdPerson;
- if (bThirdPerson)
- {
- FPSCamera->Deactivate();
- TPSCamera->Activate();
- }
- else
- {
- TPSCamera->Deactivate();
- FPSCamera->Activate();
- }
- // const FString SText = FString::Printf(TEXT("fps replicated : %d, tps replicated %d"), FPSCamera->GetIsReplicated(), TPSCamera->GetIsReplicated());
- // GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, SText);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement