Skip to content

Instantly share code, notes, and snippets.

@mikalv
Forked from pomettini/SpaceshipPawn.cpp
Created January 17, 2019 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikalv/8b4915c389341f322a0d13a44fc5112d to your computer and use it in GitHub Desktop.
Save mikalv/8b4915c389341f322a0d13a44fc5112d to your computer and use it in GitHub Desktop.
Unreal Engine 4 Custom Pawn Class to control a Spaceship
#include "SpaceshipPawn.h"
// Sets default values
ASpaceshipPawn::ASpaceshipPawn()
{
// 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;
USphereComponent * SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
RootComponent = SphereComponent;
SphereComponent->InitSphereRadius(40.0f);
SphereComponent->SetCollisionProfileName(TEXT("Pawn"));
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
Mesh->SetupAttachment(RootComponent);
// Don't rotate when the controller rotates.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
// Create a camera boom attached to the root (capsule)
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->bAbsoluteRotation = true; // Rotation of the character should not affect rotation of boom
CameraBoom->bDoCollisionTest = false;
CameraBoom->TargetArmLength = 500.f;
CameraBoom->SocketOffset = FVector(0.f, 0.f, 75.f);
CameraBoom->RelativeRotation = FRotator(0.f, 180.f, 0.f);
// Create a camera and attach to boom
SideViewCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("SideViewCamera"));
SideViewCameraComponent->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
SideViewCameraComponent->bUsePawnControlRotation = false; // We don't want the controller rotating the camera
MovementComponent = CreateDefaultSubobject<UFloatingPawnMovement>(TEXT("Movement"));
MovementComponent->UpdatedComponent = RootComponent;
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
void ASpaceshipPawn::BeginPlay()
{
Super::BeginPlay();
}
void ASpaceshipPawn::MoveUp(float Val)
{
MovementComponent->AddInputVector(FVector(0.f, 0.f, Val), true);
}
void ASpaceshipPawn::MoveRight(float Val)
{
MovementComponent->AddInputVector(FVector(0.f, -Val, 0.f), true);
}
// Called every frame
void ASpaceshipPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ASpaceshipPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveRight", this, &ASpaceshipPawn::MoveRight);
PlayerInputComponent->BindAxis("MoveUp", this, &ASpaceshipPawn::MoveUp);
}
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "GameFramework/FloatingPawnMovement.h"
#include "Components/SphereComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "SpaceshipPawn.generated.h"
UCLASS()
class ASTROBOY_API ASpaceshipPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
ASpaceshipPawn();
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
/** Side view camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* SideViewCameraComponent;
/** Camera boom positioning the camera beside the character */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* CameraBoom;
UPROPERTY(EditAnywhere)
UStaticMeshComponent * Mesh;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
virtual void MoveUp(float Val);
virtual void MoveRight(float Val);
UFloatingPawnMovement * MovementComponent;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment