Skip to content

Instantly share code, notes, and snippets.

@mossyblog
Created December 21, 2022 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mossyblog/07fc8b634ef88f278e39e6ba8be3c495 to your computer and use it in GitHub Desktop.
Save mossyblog/07fc8b634ef88f278e39e6ba8be3c495 to your computer and use it in GitHub Desktop.
Unreal C++ / Wingsuit Component
#include "WingSuitComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Components/CapsuleComponent.h"
UWingSuitComponent::UWingSuitComponent()
{
PrimaryComponentTick.bCanEverTick = true;
DescentRate = 1000.0f;
bIsWingSuiting = false;
}
void UWingSuitComponent::BeginPlay()
{
Super::BeginPlay();
// Get a reference to the character's movement and capsule components
MovementComponent = GetOwner()->FindComponentByClass<UCharacterMovementComponent>();
CapsuleComponent = GetOwner()->FindComponentByClass<UCapsuleComponent>();
}
void UWingSuitComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// Check if the character is wing suiting
if (bIsWingSuiting)
{
// Apply downward force to the character
FVector DescentVector = FVector(0.0f, 0.0f, -DescentRate * DeltaTime);
MovementComponent->AddForce(DescentVector);
// Check if the character has reached the ground
FVector Location = GetOwner()->GetActorLocation();
float CapsuleHalfHeight = CapsuleComponent->GetScaledCapsuleHalfHeight();
if (Location.Z - CapsuleHalfHeight <= CapsuleComponent->GetComponentLocation().Z)
{
// Stop wing suiting and reset flag
MovementComponent->StopMovementImmediately();
bIsWingSuiting = false;
}
}
}
void UWingSuitComponent::StartWingSuiting()
{
bIsWingSuiting = true;
}
void UWingSuitComponent::StopWingSuiting()
{
bIsWingSuiting = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment