Hi all,
I’m trying to normalize a vector received by UTankNavMovementComponent::RequestDirectMove()
, but logging the vector to the console shows that it hasn’t been normalized at all. I tried it with and without providing a float parameter in GetSafeNormal()
and I also tried GetUnsafeNormal()
.
I’m either receiving a (0, 0, 0)
vector or something huge from the normalization attempt.
A workaround that I found (but I don’t think it leads to the exact same desired behavior) is to use MoveVelocity.GetClampedToSize(-1.0f, 1.0f)
instead of normalization.
It’s really confusing me because it seems such a simple thing… Any help much appreciated! Using UE 4.23.0.
Screenshot of the console output (two AI actors):
My CPP file (TankNavMovementComponent.cpp):
#include "TankNavMovementComponent.h"
#include "DrawDebugHelpers.h"
#include "TankTrack.h"
#pragma region Overrides
void UTankNavMovementComponent::RequestDirectMove(const FVector& MoveVelocity, bool bForceMaxSpeed)
{
// No need to call Super. Logic will be replaced.
const FVector TankForwardVectorNormal = GetOwner()->GetActorForwardVector();
UE_LOG(LogTemp, Warning, TEXT("[%s] TankForwardVectorNormal: %s, IsNormalized: %s"),
*GetOwner()->GetName(),
*TankForwardVectorNormal.ToString(),
TankForwardVectorNormal.IsNormalized() ? TEXT("True") : TEXT("False"));
UE_LOG(LogTemp, Warning, TEXT("[%s] MoveVelocity: %s, IsNormalized: %s"),
*GetOwner()->GetName(),
*MoveVelocity.ToString(),
MoveVelocity.IsNormalized() ? TEXT("True") : TEXT("False"));
// TODO: Why doesn't this work when normalizing with GetSafeNormal()?
const auto MoveVelocityNormal = MoveVelocity.GetSafeNormal();
//const auto MoveVelocityNormal = MoveVelocity.GetClampedToSize(-1.0f, 1.0f);
UE_LOG(LogTemp, Warning, TEXT("[%s] MoveVelocityNormal: %s, IsNormalized: %s"),
*GetOwner()->GetName(),
*MoveVelocityNormal.ToString(),
MoveVelocityNormal.IsNormalized() ? TEXT("True") : TEXT("False"));
// Calculates the ForwardThrow.
const float ForwardThrow = FVector::DotProduct(TankForwardVectorNormal, MoveVelocityNormal);
UE_LOG(LogTemp, Warning, TEXT("[%s] ForwardThrow: %f"), *GetOwner()->GetName(), ForwardThrow);
DrawDebugLine(GetWorld(), GetOwner()->GetActorLocation(), GetOwner()->GetActorLocation() + MoveVelocityNormal, FColor::Magenta, false, -1, 0, 1);
// Applies the ForwardThrow.
IntendMoveForward(ForwardThrow);
// Calculates the IntendTurnRight. Order of vectors matters for CrossProduct!
const float RightThrow = FVector::CrossProduct(TankForwardVectorNormal, MoveVelocityNormal).GetClampedToSize(-1.0f, 1.0f).Z;
// Applies the IntendTurnRight.
IntendTurnRight(RightThrow);
}
#pragma endregion
// ... rest of file ...