Created
February 8, 2018 15:03
-
-
Save luca1337/9eeef50f88f5b4bdfefda904a5980260 to your computer and use it in GitHub Desktop.
FloatingMovementController C++ To handle 2D SideScroller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill out your copyright notice in the Description page of Project Settings. | |
#include "PawnShip.h" | |
#include "Runtime/Engine/Classes/Components/StaticMeshComponent.h" | |
#include "Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h" | |
#include "Runtime/Engine/Classes/GameFramework/SpringArmComponent.h" | |
#include "Runtime/Engine/Classes/Camera/CameraComponent.h" | |
#include "Components/InputComponent.h" | |
APawnShip::APawnShip() | |
{ | |
// 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; | |
// Don't rotate when the controller rotates. | |
bUseControllerRotationPitch = false; | |
bUseControllerRotationYaw = false; | |
bUseControllerRotationRoll = false; | |
UStaticMeshComponent* MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("RootComponent")); | |
RootComponent = MeshComponent; | |
//MeshComponent->SetCollisionProfileName(TEXT("Pawn")); | |
MeshVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation")); | |
MeshVisual->AttachTo(RootComponent); | |
USpringArmComponent* SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraAttachmentArm")); | |
SpringArm->SetupAttachment(RootComponent); | |
SpringArm->bAbsoluteRotation = true; // Rotation of th | |
SpringArm->bDoCollisionTest = false; | |
SpringArm->TargetArmLength = 500.f; | |
SpringArm->SocketOffset = FVector(0.f, 0.f, 75.f); | |
SpringArm->RelativeRotation = FRotator(0.f, 180.f, 0.f); | |
// Create a camera and attach it | |
UCameraComponent* Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("ActualCamera")); | |
Camera->SetupAttachment(SpringArm, USpringArmComponent::SocketName); | |
this->MovementComponent = CreateDefaultSubobject<UFloatingPawnMovement>(TEXT("MovComp")); | |
this->MovementComponent->UpdatedComponent = RootComponent; | |
AutoPossessPlayer = EAutoReceiveInput::Player0; | |
} | |
void APawnShip::BeginPlay() | |
{ | |
Super::BeginPlay(); | |
} | |
void APawnShip::Tick(float DeltaTime) | |
{ | |
Super::Tick(DeltaTime); | |
} | |
void APawnShip::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) | |
{ | |
Super::SetupPlayerInputComponent(PlayerInputComponent); | |
PlayerInputComponent->BindAxis("MoveRight", this, &APawnShip::MoveRight); | |
PlayerInputComponent->BindAxis("MoveUp", this, &APawnShip::MoveUp); | |
} | |
void APawnShip::MoveRight(float val) | |
{ | |
this->MovementComponent->AddInputVector(FVector(0, -val, 0), false); | |
} | |
void APawnShip::MoveUp(float val) | |
{ | |
this->MovementComponent->AddInputVector(FVector(0, 0, val), false); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Fill out your copyright notice in the Description page of Project Settings. | |
#pragma once | |
#include "CoreMinimal.h" | |
#include "GameFramework/Pawn.h" | |
#include "Components/StaticMeshComponent.h" | |
#include "GameFramework/MovementComponent.h" | |
#include "GameFramework/FloatingPawnMovement.h" | |
#include "PawnShip.generated.h" | |
UCLASS() | |
class FLOATINGTASK_API APawnShip : public APawn | |
{ | |
GENERATED_BODY() | |
public: | |
// Sets default values for this pawn's properties | |
APawnShip(); | |
protected: | |
// Called when the game starts or when spawned | |
virtual void BeginPlay() override; | |
UFloatingPawnMovement* MovementComponent; | |
public: | |
// Called every frame | |
virtual void Tick(float DeltaTime) override; | |
// Called to bind functionality to input | |
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; | |
UPROPERTY(EditAnywhere) | |
UStaticMeshComponent* MeshVisual; | |
void MoveRight(float val); | |
void MoveUp(float val); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment