99 lines
3.7 KiB
C++
99 lines
3.7 KiB
C++
#include "MyCharacter.h"
|
|
#include "GameFramework/SpringArmComponent.h"
|
|
#include "Camera/CameraComponent.h"
|
|
#include "GameFramework/Controller.h"
|
|
#include "GameFramework/CharacterMovementComponent.h"
|
|
|
|
AMyCharacter::AMyCharacter()
|
|
{
|
|
PrimaryActorTick.bCanEverTick = 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
|
|
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
|
|
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
|
|
FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
|
|
|
|
// Set base turn and look up rates
|
|
BaseTurnRate = 45.0f;
|
|
BaseLookUpRate = 45.0f;
|
|
|
|
// Don't rotate when the controller rotates. Let that just affect the camera.
|
|
bUseControllerRotationPitch = false;
|
|
bUseControllerRotationYaw = false;
|
|
bUseControllerRotationRoll = false;
|
|
|
|
// Configure character movement
|
|
GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
|
|
GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
|
|
GetCharacterMovement()->JumpZVelocity = 600.0f;
|
|
GetCharacterMovement()->AirControl = 0.2f;
|
|
}
|
|
|
|
void AMyCharacter::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
}
|
|
|
|
void AMyCharacter::Tick(float DeltaTime)
|
|
{
|
|
Super::Tick(DeltaTime);
|
|
}
|
|
|
|
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
|
|
{
|
|
Super::SetupPlayerInputComponent(PlayerInputComponent);
|
|
|
|
PlayerInputComponent->BindAxis("MoveForward", this, &AMyCharacter::MoveForward);
|
|
PlayerInputComponent->BindAxis("MoveRight", this, &AMyCharacter::MoveRight);
|
|
|
|
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
|
|
PlayerInputComponent->BindAxis("TurnRate", this, &AMyCharacter::TurnAtRate);
|
|
PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
|
|
PlayerInputComponent->BindAxis("LookUpRate", this, &AMyCharacter::LookUpAtRate);
|
|
}
|
|
|
|
void AMyCharacter::MoveForward(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 AMyCharacter::MoveRight(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);
|
|
AddMovementInput(Direction, Value);
|
|
}
|
|
}
|
|
|
|
void AMyCharacter::TurnAtRate(float Rate)
|
|
{
|
|
// calculate delta for this frame from the rate information
|
|
AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
|
|
}
|
|
|
|
void AMyCharacter::LookUpAtRate(float Rate)
|
|
{
|
|
// calculate delta for this frame from the rate information
|
|
AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
|
|
} |