I’m very new to C++ and the Unreal engine.
I am trying to recreate an old DOS game I used to play called Skyroads as my first project (after several tutorials etc)
Gameplay: Skyroads (DOS) HD - YouTube This video shows what I am trying to recreate.
So far I have a GameMode class and a PlayerPawn class. From the PlayerPawn class is a blueprint which I have used to setup the components of the player pawn. These components are a UBoxComponent (for collision), UMeshComponent (to represent the ship) and a UCameraComponent (to provide the third person view for the time being). This Blueprint is used as the default pawn class in the game mode.
So far I have been able to get my ship to move left and right, and also move forward at a variable speed. Next I would like to add jump functionality. To do this I am trying to use the AddImpulse function.
Now for the problem…
When I move the player start location to be off the ground and run the game, my ship falls back to he ground VERY slowly. However when I drag another instance of the blueprint into the level it falls perfectly fine. It appears that the pawn is experiencing less gravity when its being controlled.
Is there something obvious that I am missing, why is my controlled pawn not responding to gravity properly?
I will include the relevant .cpp and .h files below:
CodeRoadGameMode.h
#pragma once
#include "GameFramework/GameMode.h"
#include "CodeRoadGameMode.generated.h"
/**
*
*/
UCLASS()
class ACodeRoadGameMode : public AGameMode
{
GENERATED_UCLASS_BODY()
virtual void BeginPlay() OVERRIDE;
};
CodeRoadGameMode.cpp
#include "CodeRoads.h"
#include "CodeRoadGameMode.h"
#include "PlayerPawn.h"
ACodeRoadGameMode::ACodeRoadGameMode(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
static ConstructorHelpers::FObjectFinder<UBlueprint> PlayerPawnObject(TEXT("Blueprint'/Game/Blueprints/BP_playerPawn.BP_playerPawn'"));
if (PlayerPawnObject.Object != NULL)
{
DefaultPawnClass = (UClass*)PlayerPawnObject.Object->GeneratedClass;
}
}
void ACodeRoadGameMode::BeginPlay()
{
Super::BeginPlay();
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Game Mode is ACTIVE!"));
}
}
PlayerPawn.h
#pragma once
#include "GameFramework/Pawn.h"
#include "PlayerPawn.generated.h"
/**
*
*/
UCLASS()
class APlayerPawn : public APawn
{
GENERATED_UCLASS_BODY()
virtual void BeginPlay() OVERRIDE;
//Move Right and left
UFUNCTION()
void MoveRight(float Val);
//Speed Up and Down
UFUNCTION()
void SpeedUp(float Val);
UFUNCTION()
void OnJump();
//Make the static mesh component
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Mesh")
UPrimitiveComponent* MyMesh;
// Camera Component
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Camera")
UCameraComponent* MyCamera;
// Box Component
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "MoveBox")
UBoxComponent* MyMovementBox;
UPROPERTY()
float ShipSpeed;
UPROPERTY()
float MaxSpeed;
UPROPERTY()
float DeltaSpeed;
protected:
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) OVERRIDE;
};
PlayerPawn.cpp
#include "CodeRoads.h"
#include "PlayerPawn.h"
APlayerPawn::APlayerPawn(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
ShipSpeed = 0;
DeltaSpeed = 10;
MaxSpeed = 4000;
}
void APlayerPawn::SetupPlayerInputComponent(UInputComponent* InputComponent)
{
InputComponent->BindAxis("MoveRight", this, &APlayerPawn::MoveRight);
InputComponent->BindAxis("SpeedUp", this, &APlayerPawn::SpeedUp);
InputComponent->BindAction("Jump",IE_Pressed, this, &APlayerPawn::OnJump);
}
void APlayerPawn::BeginPlay()
{
Super::BeginPlay();
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("We are using the PlayerPawn"));
}
}
void APlayerPawn::MoveRight(float Val)
{
//First Create the vector that will determine what direction we move
FVector MovementVector = FVector(-ShipSpeed, 1500 * Val*(ShipSpeed/MaxSpeed) + 100*Val, 0.0f);
if (MyMovementBox)
{
MyMovementBox->SetPhysicsLinearVelocity(MovementVector);
}
}
void APlayerPawn::SpeedUp(float Val)
{
if (ShipSpeed <= MaxSpeed)
{
ShipSpeed += DeltaSpeed*Val;
if (ShipSpeed < 0){ ShipSpeed = 0;}
if (ShipSpeed > MaxSpeed){ ShipSpeed = MaxSpeed; }
if (GEngine)
{
FString Output = FString::SanitizeFloat(ShipSpeed);
GEngine->AddOnScreenDebugMessage(-1,5.f, FColor::Red, Output);
}
}
}
void APlayerPawn::OnJump()
{
MyMovementBox->AddImpulse(FVector(0.0f, 0.0f, 20000),"None",true);
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Jumping"));
}