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 "TestPawn.h"
- // Sets default values
- ATestPawn::ATestPawn() {
- // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bCanEverTick = true;
- //Create our components
- RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
- OurCameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
- OurCameraSpringArm->SetupAttachment(RootComponent);
- OurCameraSpringArm->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 20.0f), FRotator(-45.0f, 0.0f, 0.0f));
- OurCameraSpringArm->TargetArmLength = 400.f;
- OurCameraSpringArm->bEnableCameraLag = true;
- OurCameraSpringArm->CameraLagSpeed = 1000.0f;
- OurCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("GameCamera"));
- OurCamera->SetupAttachment(OurCameraSpringArm, USpringArmComponent::SocketName);
- //Take control of the default Player
- AutoPossessPlayer = EAutoReceiveInput::Player0;
- }
- // Called when the game starts or when spawned
- void ATestPawn::BeginPlay() {
- Super::BeginPlay();
- //Take control of the default Player
- AutoPossessPlayer = EAutoReceiveInput::Player0;
- }
- // Called every frame
- void ATestPawn::Tick(float DeltaTime) {
- Super::Tick(DeltaTime);
- //Zoom in if ZoomIn button is down, zoom back out if it's not
- {
- if (bZoomingIn) {
- ZoomFactor += DeltaTime / 0.5f; //Zoom in over half a second
- }
- else {
- ZoomFactor -= DeltaTime / 0.25f; //Zoom out over a quarter of a second
- }
- ZoomFactor = FMath::Clamp<float>(ZoomFactor, 0.0f, 1.0f);
- //Blend our camera's FOV and our SpringArm's length based on ZoomFactor
- OurCamera->FieldOfView = FMath::Lerp<float>(90.0f, 60.0f, ZoomFactor);
- OurCameraSpringArm->TargetArmLength = FMath::Lerp<float>(400.0f, 300.0f, ZoomFactor);
- }
- //Rotate our actor's yaw, which will turn our camera because we're attached to it
- {
- FRotator NewRotation = GetActorRotation();
- NewRotation.Yaw += CameraInput.X;
- SetActorRotation(NewRotation);
- }
- //Rotate our camera's pitch, but limit it so we're always looking downward
- {
- FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
- NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);
- OurCameraSpringArm->SetWorldRotation(NewRotation);
- }
- //Handle movement based on our "MoveX" and "MoveY" axes
- {
- if (!MovementInput.IsZero()) {
- //Scale our movement input axis values by 100 units per second
- MovementInput = MovementInput.GetSafeNormal() * 1000.0f;
- FVector NewLocation = GetNewLocation(GetActorLocation());
- SetActorLocation(NewLocation);
- }
- }
- }
- FVector ATestPawn::GetNewLocation(FVector current) {
- FVector out = current;
- out += GetActorForwardVector() * MovementInput.X;
- out += GetActorRightVector() * MovementInput.Y;
- return out;
- }
- // Called to bind functionality to input
- void ATestPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
- Super::SetupPlayerInputComponent(PlayerInputComponent);
- //Hook up events for "ZoomIn"
- InputComponent->BindAction("ZoomIn", IE_Pressed, this, &ATestPawn::ZoomIn);
- InputComponent->BindAction("ZoomIn", IE_Released, this, &ATestPawn::ZoomOut);
- //Hook up every-frame handling for our four axes
- InputComponent->BindAxis("P1MoveForward", this, &ATestPawn::MoveForward);
- InputComponent->BindAxis("P1MoveRight", this, &ATestPawn::MoveRight);
- InputComponent->BindAxis("CameraPitch", this, &ATestPawn::PitchCamera);
- InputComponent->BindAxis("CameraYaw", this, &ATestPawn::YawCamera);
- }
- //Input functions
- void ATestPawn::MoveForward(float AxisValue) {
- MovementInput.X = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
- }
- void ATestPawn::MoveRight(float AxisValue) {
- MovementInput.Y = FMath::Clamp<float>(AxisValue, -1.0f, 1.0f);
- }
- void ATestPawn::PitchCamera(float AxisValue) {
- CameraInput.Y = AxisValue;
- }
- void ATestPawn::YawCamera(float AxisValue) {
- CameraInput.X = AxisValue;
- }
- void ATestPawn::ZoomIn() {
- bZoomingIn = true;
- }
- void ATestPawn::ZoomOut() {
- bZoomingIn = false;
- }
Add Comment
Please, Sign In to add comment