Hello all,
First of all, i am noob and i am trying to to learn c++ just for fun. So, whats is my problem… I am trying to make a function that stops the running character, and turns him around after he stops.
I have a working piece of code, but i think theres a better way how to make it work, but i just can’t see it.
I will show you what i mean:
void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
check(PlayerInputComponent);
// List of Default Axis Bindings
PlayerInputComponent->BindAxis("TurnAround", this, &AMyCharacter::TurnAround);
}
namespace
{
UPathFollowingComponent* InitNavigationControl(AController& Controller)
{
AAIController* AsAIController = Cast<AAIController>(&Controller);
UPathFollowingComponent* PathFollowingComp = nullptr;
if (AsAIController)
{
PathFollowingComp = AsAIController->GetPathFollowingComponent();
}
else
{
PathFollowingComp = Controller.FindComponentByClass<UPathFollowingComponent>();
if (PathFollowingComp == nullptr)
{
PathFollowingComp = NewObject<UPathFollowingComponent>(&Controller);
PathFollowingComp->RegisterComponentWithWorld(Controller.GetWorld());
PathFollowingComp->Initialize();
}
}
return PathFollowingComp;
}
}
// FIX ME
void AMyCharacter::TurnAround(float AxisRate)
{
//
static UPathFollowingComponent* PathFollowingComponent = InitNavigationControl(*Controller);
//
static bool bIsFollowingPath = true;
if (Controller != NULL && AxisRate)
{
//
if (bIsFollowingPath)
{
// Cancel MouseClick Movement
Controller->StopMovement();
//
bIsFollowingPath = false;
}
//
if (PathFollowingComponent->GetStatus() != EPathFollowingStatus::Moving)
{
// Move the Camera around the Character
float DeltaYaw = AxisRate * BaseTurnAroundRate * UGameplayStatics::GetWorldDeltaSeconds(this);
AddControllerYawInput(DeltaYaw);
// And move the character the same way, while standing still
if (!InputComponent->GetAxisValue("MoveForward") || !InputComponent->GetAxisValue("MoveRight"))
{
FRotator NewActorRotation = GetActorRotation();
NewActorRotation.Yaw += DeltaYaw * CastChecked<APlayerController>(Controller)->InputYawScale;
SetActorRotation(NewActorRotation);
}
}
/*/ Should we done this in the Animation Blueprint instead?
if (WalkingAnimation != NULL)
{
// Get the animation object for the arms mesh
UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
if (AnimInstance != NULL)
{
GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Red, FString::Printf(TEXT("Character Velocity (X:%f, Y:%f, Z:%f)"), GetVelocity().X, GetVelocity().Y, GetVelocity().Z));
GetMesh()->PlayAnimation(WalkingAnimation, true);
//AnimInstance->Montage_Play(WalkingAnimation, 1.f, EMontagePlayReturnType::Duration);
}
}*/
}
else
{
//
bIsFollowingPath = true;
}
}
As you can see, my fuction TurnAround is bound to my project axis binding, so it’s executed every tick… that means that my static bool bIsFollowingPath is set to True every tick, when i am not pressing the keyboard button.
A) Is it possible to rewrite the function, so it doesn’t do that, and still works in the same way?
B) I am using UPathFollowingComponent here, and the only way how to get the used UPathFollowingComponent is via UPathFollowingComponent* InitNavigationControl(AController& Controller) pointer function that i copy-paste from the UAIBlueprintHelperLibrary… I had to copy-paste it, because i couldn’t find an accessible version. I also write it as static in the TurnAround function, i hope that is not a problem?
C) I am using a casting there, which i don’t understand much, but since its a another copy-paste, i think it’s ok?
D) I also want to add an animation while the character is turning, but as i wrote in the commented section, i believe i should done that in the animation bleuprint, not here.