This is my .h Code
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Character.h"
#include "Penmen.generated.h"
UCLASS()
class GUIDED_EXERCISE1_API APenmen : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
APenmen();
protected:
void EndSprint();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
//This sets up the methods for character movement and will grab the Value (1.0 for moving forward and -1.0 for moving backward from Unreal
//for how fast to move when the corresponding key is pressed
void MoveForward(float Value);
//This sets up the methods for character movement and will grab the Value (1.0 for moving right and -1.0 for moving left from Unreal
//for how fast to move when the corresponding key is pressed
void MoveRight(float Value);
void OnStartSprint(float Value);
void OnEndSprint(float Value);
};
An here is my .cpp code:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Guided_Exercise1.h"
#include "Penmen.h"
// Sets default values
APenmen::APenmen()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
GetCharacterMovement()->JumpZVelocity = 600.f;
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->MaxWalkSpeed = 5400.f;
}
// Called when the game starts or when spawned
void APenmen::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void APenmen::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void APenmen::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
//This code maps the forward, back, right, and left movement to the W, A, S, D keys that were assigned under the details section
//of character movement in Unreal. The values and key information is assigned in the Unreal Engine under Detail of the Character Movement pane
PlayerInputComponent->BindAxis("MoveForward_Key", this, &APenmen::MoveForward);
PlayerInputComponent->BindAxis("MoveRight_Key", this, &APenmen::MoveRight);
//This code makes the character jump by pressing the spacebar
PlayerInputComponent->BindAction("Jump_Key", IE_Pressed, this, &ACharacter::Jump);
PlayerInputComponent->BindAction("Jump_Key", IE_Released, this, &ACharacter::StopJumping);
PlayerInputComponent->BindAction("Run_Key", IE_Pressed, this, &APenmen::OnStartSprint);
PlayerInputComponent->BindAction("Run_Key", IE_Released, this, &APenmen::OnEndSprint);
}
//This method makes the character move forward and backward by pressing the W and S key
//which were assigned in the details section of character movement in Unreal
void APenmen::MoveForward(float Value) {
//UE_LOG(LogTemp, Warning, TEXT("W or S Key pressed"));
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
//By using the X axis, the character will now move forward and back. It will scale along the X axis by moving forward, which
//has a value of 1.0, and backward, which has a value 0f -1.0. Visual Studio recieves these values from the Unreal Engine
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Value);
}
//This method makes the character move left and right by using the A and S key
void APenmen::MoveRight(float Value) {
//UE_LOG(LogTemp, Warning, TEXT("D or A Key Pressed"));
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
//By using the Y axis, the character will move along the Y axis which is left and right
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
AddMovementInput(Direction, Value);
}
void APenmen::OnStartSprint(float Value)
{
GetCharacterMovement()->MaxWalkSpeed = 5400.f;
}
void APenmen::OnEndSprint(float Value)
{
GetCharacterMovement()->MaxWalkSpeed = 50.f;
}
Thank you for your help.