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 "Net/UnrealNetwork.h"
- #include "GameFramework/CharacterMovementComponent.h"
- #include "GameFramework/SpringArmComponent.h"
- // Sets default values
- APlayerCharacter::APlayerCharacter()
- {
- // Configure character movements
- GetCharacterMovement()->bOrientRotationToMovement = false;
- 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;
- // 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
- TPSCamera->bAutoActivate = false;
- // Base configuration for camera view
- bThirdPerson = false;
- bUseControllerRotationYaw = true;
- bUseControllerRotationRoll = true;
- }
- // Called when the game starts or when spawned
- void APlayerCharacter::BeginPlay()
- {
- Super::BeginPlay();
- }
- void APlayerCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty> & OutLifetimeProps) const {
- Super::GetLifetimeReplicatedProps(OutLifetimeProps);
- DOREPLIFETIME(APlayerCharacter, bThirdPerson);
- }
- // 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;
- }
- void APlayerCharacter::ChangeView()
- {
- bThirdPerson = !bThirdPerson;
- Server_SetThirdParty(bThirdPerson);
- OnRep_ChangeView();
- }
- // Switch from TPS to FPS
- void APlayerCharacter::OnRep_ChangeView()
- {
- bUseControllerRotationYaw = !bThirdPerson;
- bUseControllerRotationRoll = !bThirdPerson;
- GetCharacterMovement()->bOrientRotationToMovement = bThirdPerson;
- if (bThirdPerson)
- {
- FPSCamera->Deactivate();
- TPSCamera->Activate();
- }
- else
- {
- TPSCamera->Deactivate();
- FPSCamera->Activate();
- }
- }
- void APlayerCharacter::Server_SetThirdParty_Implementation(bool IsThirdPerson)
- {
- bThirdPerson = IsThirdPerson;
- OnRep_ChangeView();
- }
Add Comment
Please, Sign In to add comment