Skip to content

Instantly share code, notes, and snippets.

@AltimorTASDK
Created April 25, 2024 06:39
Show Gist options
  • Save AltimorTASDK/8e4ffaf29c041e2646e51568c27ca769 to your computer and use it in GitHub Desktop.
Save AltimorTASDK/8e4ffaf29c041e2646e51568c27ca769 to your computer and use it in GitHub Desktop.
void UCharacterMovementComponent::JumpOff(AActor* MovementBaseActor)
{
if ( !bPerformingJumpOff )
{
bPerformingJumpOff = true;
if ( CharacterOwner )
{
const float MaxSpeed = GetMaxSpeed() * 0.85f;
Velocity += MaxSpeed * GetBestDirectionOffActor(MovementBaseActor);
if ( Velocity.Size2D() > MaxSpeed )
{
Velocity = MaxSpeed * Velocity.GetSafeNormal();
}
if (HasCustomGravity())
{
FVector GravityRelativeVelocity = RotateWorldToGravity(Velocity);
GravityRelativeVelocity.Z = JumpOffJumpZFactor * JumpZVelocity;
Velocity = RotateGravityToWorld(GravityRelativeVelocity);
}
else
{
Velocity.Z = JumpOffJumpZFactor * JumpZVelocity;
}
SetMovementMode(MOVE_Falling);
}
bPerformingJumpOff = false;
}
}
FVector UCharacterMovementComponent::GetBestDirectionOffActor(AActor* BaseActor) const
{
// By default, just pick a random direction. Derived character classes can choose to do more complex calculations,
// such as finding the shortest distance to move in based on the BaseActor's Bounding Volume.
const float RandAngle = FMath::DegreesToRadians(GetNetworkSafeRandomAngleDegrees());
return FVector(FMath::Cos(RandAngle), FMath::Sin(RandAngle), 0.5f).GetSafeNormal();
}
float UCharacterMovementComponent::GetNetworkSafeRandomAngleDegrees() const
{
float Angle = RandomStream.FRand() * 360.f;
if (!IsNetMode(NM_Standalone))
{
// Networked game
// Get a timestamp that is relatively close between client and server (within ping).
FNetworkPredictionData_Server_Character const* ServerData = (HasPredictionData_Server() ? GetPredictionData_Server_Character() : NULL);
FNetworkPredictionData_Client_Character const* ClientData = (HasPredictionData_Client() ? GetPredictionData_Client_Character() : NULL);
float TimeStamp = Angle;
if (ServerData)
{
TimeStamp = ServerData->CurrentClientTimeStamp;
}
else if (ClientData)
{
TimeStamp = ClientData->CurrentTimeStamp;
}
// Convert to degrees with a faster period.
const float PeriodMult = 8.0f;
Angle = TimeStamp * PeriodMult;
Angle = FMath::Fmod(Angle, 360.f);
}
return Angle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment