Skip to content

Instantly share code, notes, and snippets.

@MattiaPezzanoAiv
Created February 10, 2018 10:30
Show Gist options
  • Save MattiaPezzanoAiv/720adcf37fd6e4dde84b805b3bfd05f7 to your computer and use it in GitHub Desktop.
Save MattiaPezzanoAiv/720adcf37fd6e4dde84b805b3bfd05f7 to your computer and use it in GitHub Desktop.
#include "AstroPawn.h"
#include "Components/InputComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
// Sets default values
AAstroPawn::AAstroPawn()
{
// 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;
//UGameplayStatics::GetPlayerController(GetWorld(), 0)->Possess(this);
// Don't rotate when the controller rotates.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
RootComponent = SphereComponent;
//add static mesh component
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(FName("Static Mesh Component")); //assign default mesh from c++???
StaticMesh->SetupAttachment(RootComponent);
//GetOwner()->SetRootComponent(StaticMesh);
// Create a camera boom attached to the root (capsule)
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->bInheritPitch = false;
CameraBoom->bInheritYaw = false;
CameraBoom->bInheritRoll = false;
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("Camera"));
SideViewCameraComponent->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);
SideViewCameraComponent->bUsePawnControlRotation = false;
MovementComponent = CreateDefaultSubobject<UFloatingPawnMovement>(TEXT("Floating Pawn Movement"));
MovementComponent->UpdatedComponent = RootComponent;
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
void AAstroPawn::BeginPlay()
{
Super::BeginPlay();
CameraBoom->SetRelativeLocation(FVector(0, 0, 0));
}
// Called every frame
void AAstroPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
Accumulator += DeltaTime*5;
}
// Called to bind functionality to input
void AAstroPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveRight", this, &AAstroPawn::MoveRight);
PlayerInputComponent->BindAxis("MoveUp", this, &AAstroPawn::MoveUp);
}
void AAstroPawn::MoveRight(float Value)
{
// add movement in that direction
MovementComponent->AddInputVector(FVector(0, -1, 0)*Value,true);
}
void AAstroPawn::MoveUp(float Value)
{
float Delta = FMath::Sin(Accumulator);
// add movement in that direction
MovementComponent->AddInputVector(FVector(0, 0, 1)*Value + FVector(0,0,Delta), true);
}
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Runtime/Engine/Classes/Components/StaticMeshComponent.h"
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"
#include "Runtime/Engine/Classes/GameFramework/Actor.h"
#include "Components/SphereComponent.h"
#include "GameFramework/FloatingPawnMovement.h"
#include "AstroPawn.generated.h"
UCLASS()
class EXERCISE20180208_API AAstroPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AAstroPawn();
UPROPERTY(EditAnywhere)
UStaticMeshComponent* StaticMesh;
/** 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;
/** Returns SideViewCameraComponent subobject **/
FORCEINLINE class UCameraComponent* GetSideViewCameraComponent() const { return SideViewCameraComponent; }
/** Returns CameraBoom subobject **/
FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
void MoveRight(float Val);
void MoveUp(float Val);
UPROPERTY(EditAnywhere)
UFloatingPawnMovement* MovementComponent;
float Accumulator;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment