After utilising the sweep stuff defined in this post:
Still seeing janky collisions and not sure why … perhaps I’ve misunderstood. Any thoughts?
// Called every frame
void AMovingPlatform::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// if stationary there is no movement necessary
if ( MaxDistance == 0.0 )
return;
FVector const NewLocation = GetActorLocation() + (PlatformVelocity*DeltaTime);
FHitResult Hit;
SetActorLocation(NewLocation, true, &Hit);
if ( Hit.GetActor() )
{
UpdatePushedLocation(Hit);
SetActorLocation(NewLocation);
}
DistanceMoved = FVector::Dist(AnchorLocation,NewLocation);
if ( DistanceMoved > MaxDistance )
{
// We are moving the new anchor to be the start + *exactly* the max distance to guarantee Dist() is 0
AnchorLocation = AnchorLocation + ( PlatformVelocity.GetSafeNormal() * MaxDistance );
SetActorLocation(AnchorLocation);
PlatformVelocity = -PlatformVelocity;
}
}
// updates the item that there is overlap with
void AMovingPlatform::UpdatePushedLocation(const FHitResult& HitResult )
{
const double Distance = FVector::Dist(HitResult.TraceStart, HitResult.TraceEnd);
const FVector Direction = PlatformVelocity.GetSafeNormal();
const FVector Delta = Direction * Distance;
HitResult.GetActor()->AddActorWorldOffset(Delta);
}```