My goal is to make movement only in player controller and not the character class.
I have a separate project where I accomplished this 100% when I try to do it it with my new project. Character will not move and is stuck in the air.
// Fill out your copyright notice in the Description page of Project Settings.
#include "Controllers/BasePlayerController.h"
#include <Kismet/GameplayStatics.h>
#include <Player/PlayerCharacter.h>
#include "../RPGSIM.h"
ABasePlayerController::ABasePlayerController()
{
//IgnoreMoveInput = 0;
//IgnoreLookInput = 0;
//AutoReceiveInput = EAutoReceiveInput::Player0;
//SetIgnoreLookInput(false);
//SetIgnoreMoveInput(false);
}
void ABasePlayerController::MoveForward(float Value) {
if (PlayerPawn == nullptr) {
class APawn* TempPawn = GetPawn();
if (TempPawn != nullptr) {
PlayerPawn = Cast<APlayerCharacter>(TempPawn);
}
}
if ((Value != 0.0f))
{
// find out which way is forward
const FRotator Rotation = PlayerPawn->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
PlayerPawn->AddMovementInput(Direction, Value);
}
}
void ABasePlayerController::MoveRight(float Value) {
if (PlayerPawn == nullptr) {
class APawn* TempPawn = GetPawn();
if (PlayerPawn != nullptr) {
PlayerPawn = Cast<APlayerCharacter>(TempPawn);
}
}
if ((Value != 0.0f))
{
// find out which way is right
const FRotator Rotation = PlayerPawn->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get right vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// add movement in that direction
PlayerPawn->AddMovementInput(Direction, Value);
}
}
void ABasePlayerController::TurnAtRate(float Rate)
{
if (PlayerPawn) {
PlayerPawn->AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
}
}
void ABasePlayerController::LookUpAtRate(float Rate)
{
if (PlayerPawn) {
PlayerPawn->AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
}
}
void ABasePlayerController::BeginPlay()
{
/*When Game Starts Grab Player Character*/
Super::BeginPlay();
class APawn* TempPawn = GetPawn();
if (TempPawn != nullptr) {
PlayerPawn = Cast<APlayerCharacter>(TempPawn);
if (PlayerPawn) {
DEBUG_MESSAGE("Cast Success", 5.f);
AttachToPawn(PlayerPawn);
}
}
else {
UE_LOG(LogTemp, Fatal, TEXT("Player is not controlled or missing."));
}
}
void ABasePlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
/*
if (InputComponent == NULL)
{
InputComponent = NewObject<UInputComponent>(this, TEXT("Player_InputComponent"));
InputComponent->RegisterComponent();
}*/
InputComponent->BindAxis("MoveForward", this, &ABasePlayerController::MoveForward);
InputComponent->BindAxis("MoveRight", this, &ABasePlayerController::MoveRight);
InputComponent->BindAxis("TurnAtRate", this, &ABasePlayerController::TurnAtRate);
InputComponent->BindAxis("LookUpRate", this, &ABasePlayerController::LookUpAtRate);
}
---
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "BasePlayerController.generated.h"
/**
*
*/
UCLASS()
class RPGSIM_API ABasePlayerController : public APlayerController
{
GENERATED_BODY()
public:
ABasePlayerController();
void MoveForward(float Value);
void MoveRight(float Value);
void TurnAtRate(float Rate);
void LookUpAtRate(float Rate);
float BaseTurnRate = 45.f;
float BaseLookUpRate = 45.f;
protected:
virtual void BeginPlay() override;
virtual void SetupInputComponent() override;
private:
class APlayerCharacter* PlayerPawn;
};
----------
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "BaseUnit.generated.h"
UCLASS()
class RPGSIM_API ABaseUnit : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
ABaseUnit();
protected:
// 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;
void MoveForward(float Value);
void MoveRight(float Value);
class ABasePlayerController* DefaultController;
};
---
// Fill out your copyright notice in the Description page of Project Settings.
#include "Base/BaseUnit.h"
#include "../RPGSIM.h"
#include <Controllers/BasePlayerController.h>
// Sets default values
ABaseUnit::ABaseUnit()
{
// 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;
//GetMesh()->bEnablePhysicsOnDedicatedServer = true;
}
// Called when the game starts or when spawned
void ABaseUnit::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ABaseUnit::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ABaseUnit::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(PlayerInputComponent);
DEBUG_MESSAGE(__FUNCTION__, 5.f);
PlayerInputComponent->BindAxis("MoveForward", this, &ABaseUnit::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &ABaseUnit::MoveRight);
}
void ABaseUnit::MoveForward(float Value)
{
if ((Controller != nullptr) && (Value != 0.0f))
{
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Value);
}
}
void ABaseUnit::MoveRight(float Value)
{
if ((Controller != nullptr) && (Value != 0.0f))
{
// find out which way is right
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get right vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// add movement in that direction
AddMovementInput(Direction, Value);
}
}
------------
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Base/BaseUnit.h"
#include "PlayerCharacter.generated.h"
/**
*
*/
UCLASS()
class RPGSIM_API APlayerCharacter : public ABaseUnit
{
GENERATED_BODY()
public:
APlayerCharacter();
/*Class Declarations */
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Player | Camera")
class USpringArmComponent* SpringArm;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Player | Camera")
class UCameraComponent* Camera;
/*Values */
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite , Category = "Player | Camera")
float SpringLength = 450.f;
/*Functions */
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void MoveForward(float Value);
void MoveRight(float Value);
FORCEINLINE class USpringArmComponent* GetSpringArm() const { return SpringArm; }
FORCEINLINE class UCameraComponent* GetCamera() const { return Camera; }
};
------
// Fill out your copyright notice in the Description page of Project Settings.
#include "Player/PlayerCharacter.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "Components/CapsuleComponent.h"
#include "Engine/SkeletalMesh.h"
#include "../RPGSIM.h"
#include <GameFramework/Character.h>
APlayerCharacter::APlayerCharacter()
{
PrimaryActorTick.bCanEverTick = true;
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm"));
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
//setup
SpringArm->SetupAttachment(GetRootComponent());
SpringArm->TargetArmLength = SpringLength;
SpringArm->bUsePawnControlRotation = true;
SpringArm->bDoCollisionTest = false;
Camera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
//HUMAN MALE Capsule INFO
// Half Height 63.357975
// Radius 31.252357
GetCapsuleComponent()->SetCapsuleSize(63.35f, 31.36f);
static ConstructorHelpers::FObjectFinder<USkeletalMesh>
SkeletalMeshAsset(TEXT("SkeletalMesh'/Game/Main/Skeletal/Humans/Male/Player/base_human_male_01.base_human_male_01'"));
if (SkeletalMeshAsset.Succeeded())
{
GetMesh()->SetSkeletalMesh(SkeletalMeshAsset.Object);
}
else {
}
//Movement related
GetCharacterMovement()->bOrientRotationToMovement = true;
GetCharacterMovement()->RotationRate = FRotator(0.f, 240.f, 0.f);
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
void APlayerCharacter::BeginPlay()
{
}
void APlayerCharacter::MoveForward(float Value)
{
if (Controller != nullptr && Value != 0.0f) {
// find out which way is right
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
DEBUG_MESSAGE(FString::SanitizeFloat(Value), 5.f);
// get forward vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Value);
}
else {
}
}
void APlayerCharacter::MoveRight(float Value)
{
if (Controller != nullptr && Value != 0.0f) {
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
DEBUG_MESSAGE(FString::SanitizeFloat(Rotation.Yaw));
// get right vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// add movement in that direction
DEBUG_MESSAGE(FString::SanitizeFloat(Value), 5.f);
AddMovementInput(Direction, Value);
}
else {
}
}
void APlayerCharacter::Tick(float DeltaTime)
{
}