Hello,
I have a problem with my Unit Movement. I am working on an RTS and I am trying to implement the Detour Crowd Avoidance provided by Unreal Engine but run into some problems.
- If 2 Units collide(what shouldnt even happen) 1 Unit pushes the other unit away like a rocket.
- Sometimes the pathfinding is weird. The unit first walks in a different direction then it should.
I first tried it with a blueprint project (3rd person template) and simply changed the AIController to a DetourCrowdAIController and it worked fine. No pushing or anything. So it must have to do something with my implementation in C++, but I dont know what. It has to be either the AIController or the Unit itself.
Here is the code of the Controller and the Unit and also a short video:
My Controller that every Unit has
#pragma once
#include "AIController.h"
#include "RTSUnitController.generated.h"
/**
*
*/
UCLASS()
class RTSCAMERAANDMOVEMENT_API ARTSUnitController : public AAIController
{
GENERATED_BODY()
public:
ARTSUnitController(const FObjectInitializer& ObjectInitializer);
};
#include "RTSCameraAndMovement.h"
#include "RTSUnitController.h"
#include "Navigation/CrowdFollowingComponent.h"
ARTSUnitController::ARTSUnitController(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer.SetDefaultSubobjectClass<UCrowdFollowingComponent>(TEXT("PathFollowingComponent")))
{
}
My implementation of a Basic Unit
#include "GameFramework/Character.h"
#include "RTSUnitController.h"
#include "RTSUnit.generated.h"
UCLASS()
class RTSCAMERAANDMOVEMENT_API ARTSUnit : public ACharacter
{
GENERATED_BODY()
public:
//Static Mesh Component
UPROPERTY(EditAnywhere)
UStaticMeshComponent* UnitMesh;
//Selection Circle
UPROPERTY(EditAnywhere)
UStaticMeshComponent* SelectionMesh;
//UnitSelected
void UnitSelected();
void UnitDeselected();
// Sets default values for this character's properties
ARTSUnit();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
};
#include "RTSCameraAndMovement.h"
#include "RTSUnit.h"
// Sets default values
ARTSUnit::ARTSUnit()
{
// 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;
//Set Default AI Controller
AIControllerClass = ARTSUnitController::StaticClass();
//Visible Mesh
UnitMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
UnitMesh->AttachTo(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> UnitMeshAsset(TEXT("/Game/Models/Archer.Archer"));
if (UnitMeshAsset.Succeeded())
{
UnitMesh->SetStaticMesh(UnitMeshAsset.Object);
}
//Selection Mesh
SelectionMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Selection"));
SelectionMesh->AttachTo(UnitMesh);
SelectionMesh->SetRelativeScale3D(FVector(2,2,2));
SelectionMesh->SetVisibility(false);
static ConstructorHelpers::FObjectFinder<UStaticMesh> UnitSelectionAsset(TEXT("/Game/Models/Shape_Plane.Shape_Plane"));
if (UnitSelectionAsset.Succeeded())
{
SelectionMesh->SetStaticMesh(UnitSelectionAsset.Object);
}
}
// Called when the game starts or when spawned
void ARTSUnit::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ARTSUnit::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
// Called to bind functionality to input
void ARTSUnit::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
}
void ARTSUnit::UnitSelected()
{
SelectionMesh->SetVisibility(true);
}
void ARTSUnit::UnitDeselected()
{
SelectionMesh->SetVisibility(false);
}
Sorry for the laggy video but I am currently on my Notebook during holidays and dont have access to my pc
Maybe someone has a solution.