Files
ProjectEleri/Source/ProjectEleri/NPC/ExtendedPawnMovement.cpp
2026-07-11 10:43:52 +02:00

82 lines
2.2 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "ExtendedPawnMovement.h"
DEFINE_STAT(STAT_ExtendedPawnMovement_Tick);
UExtendedPawnMovement::UExtendedPawnMovement() {
}
void UExtendedPawnMovement::RequestPathMove(const FVector& MoveInput) {
RequestDirectMove(MoveInput, false);
}
void UExtendedPawnMovement::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
SCOPE_CYCLE_COUNTER(STAT_ExtendedPawnMovement_Tick);
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
/*Velocity = FVector::ZeroVector;
if (!RequestedVelocity.IsNearlyZero())
Velocity += RequestedVelocity;*/
float VerticalVelocity = 0.f;
if (PawnOwner && UpdatedComponent && bShouldApplyGravity) {
FHitResult OutHit;
bIsGrounded = TickGroundDetection(OutHit);
if (!bIsGrounded)
{
VerticalVelocity = GravityScale * 100.f;
}
else {
VerticalVelocity = 0;
}
}
if (!Velocity.IsNearlyZero()) {
const FRotator CurrentRotation = UpdatedComponent->GetComponentRotation();
FRotator TargetRotation = Velocity.ToOrientationRotator();
TargetRotation.Pitch = 0.f;
TargetRotation.Roll = 0.f;
const FRotator NewRotation = FMath::RInterpTo(
CurrentRotation,
TargetRotation,
DeltaTime,
RotationSpeed / 360.f // normalize speed
);
UpdatedComponent->SetWorldRotation(NewRotation);
}
FVector FinalVelocity = Velocity;
FinalVelocity.Z += VerticalVelocity;
MoveUpdatedComponent(FinalVelocity * DeltaTime, UpdatedComponent->GetComponentRotation(), true);
}
bool UExtendedPawnMovement::TickGroundDetection(const FHitResult& OutHit) {
//Trace for on ground
FHitResult Hit;
FVector Start = UpdatedComponent->GetComponentLocation();
float Scale = UpdatedComponent->GetLocalBounds().GetBox().GetSize().Z / 2.f;
FVector End = Start + FVector(0, 0, -Scale); // small downward trace
FCollisionQueryParams Params;
Params.AddIgnoredActor(GetOwner());
//DrawDebugLine(GetWorld(), Start, End, FColor::Red);
//DrawDebugSphere(GetWorld(), End, 30.f, 16, FColor::Blue);
return GetWorld()->SweepSingleByChannel(
Hit,
Start,
End,
FQuat::Identity,
ECC_Visibility,
FCollisionShape::MakeSphere(30.f), // or use a box/capsule
Params
);
}