Created
December 21, 2022 08:53
-
-
Save mossyblog/07fc8b634ef88f278e39e6ba8be3c495 to your computer and use it in GitHub Desktop.
Unreal C++ / Wingsuit Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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