I’m trying to make my own custom pawn movement component and I’m trying to make a step up functionality for it. But for some reason, when I’m using the SafeMoveUpdatedComponent, it ocassionally returns a random height for the impact point.
I know I can make multiple line tracing to get an accurate height of the floor but I would like it if there is another way that is more efficient or if someone can point out if something is wrong with my code.
void UHunter_MovementComponent::TickGroundMovement(float deltaTime, FVector inputVector, enum ELevelTick tickType, FActorComponentTickFunction* tickFunction)
{
currentFloor.Clear(); // clear floor information
FVector frameVelocity = Velocity;
frameVelocity.Z = 0.0f;
float frameSpeed = frameVelocity.Size();
frameSpeed += !inputVector.IsNearlyZero() ? inputVector.Size() * baseWalkAcceleration * deltaTime : -1 * baseWalkAcceleration * deltaTime;
frameSpeed = FMath::Clamp(frameSpeed, 0.0f, maxWalkVelocity);
if (FindFloor(currentFloor, GetComponentWorldSpaceTransform(UpdatedComponent).GetTranslation(), maxStepHeight) && !frameVelocity.IsNearlyZero())
{
frameVelocity = CalculateFloorSlidingVector(currentFloor.hitResult.ImpactNormal, inputVector) * frameSpeed * deltaTime;
FHitResult hit;
SafeMoveUpdatedComponent(frameVelocity, UpdatedComponent->GetComponentQuat(), true, hit);
if (hit.IsValidBlockingHit())
{
FHitResult temp;
TraceLineSingleByProfile(hit.ImpactPoint, hit.ImpactPoint + (hit.ImpactNormal * 100.0f), temp, TEXT("Pawn"), TArray<AActor*>(), EDrawDebugTrace::ForDuration);
SlideAlongSurface(frameVelocity, 1.0f - hit.Time, hit.Normal, hit);
}
}
Velocity = inputVector * frameSpeed;
}
I have a TraceLineSingleByProfile() function which basically calls UKismetSystemLibrary::LineTraceSingleByProfile() to draw a line from the hit.ImpactPoint outwards from hit.ImpactNormal and the result is shown below.