UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class SONICUNLEASHEDREMAKE_API UBoostComponent : public UActorComponent
{
class ASonicCharacterComponent* SonicCharacter;
GENERATED_BODY()
//these are the delegates that we will use to play the boost sound and particles
//these basically are the in blueprint events
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnBoostEvent);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnAirBoostEvent);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FBoostingEvent); // this one will tick every tick that we are boosting
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnStopBoostEvent);
code
//events to be accessed in the Blueprints
UPROPERTY(BlueprintAssignable, Category = "Boost")
FOnBoostEvent OnBoostEvent;
UPROPERTY(BlueprintAssignable, Category = "Boost")
FOnAirBoostEvent OnAirBoostEvent;
UPROPERTY(BlueprintAssignable, Category = "Boost")
FOnStopBoostEvent OnStopBoostEvent;
UPROPERTY(BlueprintAssignable, Category = "Boost")
FBoostingEvent BoostingEvent;
public:
// Sets default values for this component's properties
UBoostComponent();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
code
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Boost")
float BoostPower;
UFUNCTION(BlueprintCallable, Category = "Boost")
void BoostPressed();
UFUNCTION(BlueprintCallable, Category = "Boost")
void BoostReleased();
void BoostHandler();
void AirBoosting();
UFUNCTION(BlueprintCallable, Category = "Boost")
void EndAirBoostAnimation();
UFUNCTION(BlueprintCallable, Category = "Boost")
bool IsAllowedToBoost() const;
UFUNCTION(BlueprintCallable, Category = "Boost")
void EnableAirBoosting();
UFUNCTION(BlueprintCallable, Category = "Boost")
void DisableAirBoosting();
UFUNCTION(BlueprintCallable, Category = "Boost")
bool IsBoosting() const { return bIsBoosting; }
float BoostExtraSpeedConstant;
FTimerHandle AirBoostAnimationTimer;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Boost")
bool bIsBoosting;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Boost")
bool bHasAirBoosted; //you will never actually be in the state of airboosting, because it only triggers once
Assistant
};
// Fill out your copyright notice in the Description page of Project Settings.
// Sets default values for this component's properties
UBoostComponent::UBoostComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
BoostPower = 100.0f;
BoostExtraSpeedConstant = 2000.0f;
bIsBoosting = false; //this is a toggle boolean
bHasAirBoosted = false;
}
// Called when the game starts
void UBoostComponent::BeginPlay()
{
Super::BeginPlay();
SonicCharacter = Cast(GetOwner());
}
// Called every frame
void UBoostComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
BoostHandler();
}
void UBoostComponent::BoostPressed()
{
//we play sound and particles and camera shake in the blueprint
//todo check if we can even boost
//todo add boost meter implementation
code
//only boost if we are not currently boosting, to prevent double inputs
if (bIsBoosting || bHasAirBoosted) return;
if (SonicCharacter->SonicMovement->IsFalling())
{
AirBoosting();
return;
}
OnBoostEvent.Broadcast();
bIsBoosting = true;
const float MissingSpeedFromMax = SonicCharacter->SonicMovement->GetMaxSpeed() - SonicCharacter->SonicMovement->Velocity.Size();
const FVector BoostDirection = SonicCharacter->GetActorForwardVector() * MissingSpeedFromMax;
SonicCharacter->SonicMovement->Velocity += BoostDirection;
SonicCharacter->SonicMovement->MaxWalkSpeed = SonicCharacter->SonicMovement->GetMaxSpeed() + BoostExtraSpeedConstant;
SonicCharacter->SonicMovement->StopSlide();
}
void UBoostComponent::BoostHandler()
{
if (!bIsBoosting)
return;
if (!IsAllowedToBoost())
{
BoostReleased(); // force release boost if not allowed anymore
return;
}
//boost!
SonicCharacter->AddMovementInput(SonicCharacter->GetActorForwardVector(), 2.0f);
}
void UBoostComponent::AirBoosting()
{
bHasAirBoosted = true;
OnAirBoostEvent.Broadcast();
const float MissingSpeedFromMax = SonicCharacter->GetCharacterMovement()->GetMaxSpeed() - SonicCharacter->GetCharacterMovement()->Velocity.Size();
const FVector BoostDirection = SonicCharacter->GetActorForwardVector() * MissingSpeedFromMax;
SonicCharacter->GetCharacterMovement()->Velocity += BoostDirection;
//this sets the state to airboosting for 0.3 seconds, so we can play the animation
SonicCharacter->CurrentState = EFEeSonicCharacterState::AirBoosting;
GetWorld()->GetTimerManager().SetTimer(AirBoostAnimationTimer, this, &UBoostComponent::EndAirBoostAnimation, 0.3f, false);
SonicCharacter->HomingAttackComponent->AirDashDisable();
if (SonicCharacter->CurrentState == EFEeSonicCharacterState::Jumping)
{
SonicCharacter->CurrentState = EFEeSonicCharacterState::Falling;
}
bool UBoostComponent::IsAllowedToBoost() const
{
//check if we are in the air
const bool AirCheck = !SonicCharacter->GetCharacterMovement()->IsFalling();
const bool SpeedCheck = SonicCharacter->GetCharacterMovement()->Velocity.Size() > 500.0f; // todo 500 is the boosting min speed, make it a variable
return AirCheck && SpeedCheck;
}
void UBoostComponent::BoostReleased()
{
if (!bIsBoosting)
{
return;
}
bIsBoosting = false;
//reset the walk speed
SonicCharacter->SonicMovement->MaxWalkSpeed = SonicCharacter->SonicMovement->GetMaxSpeed() - BoostExtraSpeedConstant;
OnStopBoostEvent.Broadcast();
}
assume sonic unleashed / sonic generations 3d movement system.
can you finish and improve this boost component?
To enhance and complete your UBoostComponent for the Sonic character in Unreal Engine, we'll address several areas:
Code Structure and Best Practices:
GENERATED_BODY Placement: In Unreal Engine, GENERATED_BODY() should be the first line inside the class body to ensure proper code generation.
Access Specifiers: Properly organize variables and functions under public, protected, and private sections for better encapsulation.
Pointer Safety: Ensure that pointers like SonicCharacter are valid before usage to prevent potential crashes.
Property Visibility and Configurability: Expose relevant properties to Blueprints and make constants configurable where appropriate.
Functional Enhancements:
Boosting Event: Implement the BoostingEvent delegate to broadcast boost status every tick while boosting.
Boost Constraints: Refine conditions under which boosting is allowed, such as checking stamina or boost meters if applicable.
Movement Handling: Improve how boost affects character movement to ensure smooth and expected behavior.
State Management: Ensure that character states are accurately managed during boosting and air boosting.
Code Completion:
Boost Meter Implementation: Although marked as TODO, a boost meter can be integrated for better gameplay mechanics.
Air Boost Animation: Ensure that air boost animations are correctly triggered and terminated.
Below is the revised and improved version of both your header (BoostComponent.h) and implementation (BoostComponent.cpp) files with detailed comments and enhancements.
BoostComponent.h
cpp
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "BoostComponent.generated.h"
class ASonicCharacter; // Forward declaration
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class SONICUNLEASHEDREMAKE_API UBoostComponent : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UBoostComponent();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
// Boost Power can be adjusted in Blueprints
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Boost")
float BoostPower;
// Extra speed added during boost
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Boost")
float BoostExtraSpeed;
// Min speed required to initiate boost
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Boost")
float MinimumBoostSpeed;
// Boost duration (optional, if boosting is time-limited)
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Boost")
float BoostDuration;
// Events to be accessed in the Blueprints
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnBoostEvent);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnAirBoostEvent);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FBoostingEvent); // This one will tick every frame that we are boosting
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnStopBoostEvent);
UPROPERTY(BlueprintAssignable, Category = "Boost")
FOnBoostEvent OnBoostEvent;
UPROPERTY(BlueprintAssignable, Category = "Boost")
FOnAirBoostEvent OnAirBoostEvent;
UPROPERTY(BlueprintAssignable, Category = "Boost")
FOnStopBoostEvent OnStopBoostEvent;
UPROPERTY(BlueprintAssignable, Category = "Boost")
FBoostingEvent BoostingEvent;
// Boost control functions
UFUNCTION(BlueprintCallable, Category = "Boost")
void BoostPressed();
UFUNCTION(BlueprintCallable, Category = "Boost")
void BoostReleased();
// Air boost control
UFUNCTION(BlueprintCallable, Category = "Boost")
void EnableAirBoosting();
UFUNCTION(BlueprintCallable, Category = "Boost")
void DisableAirBoosting();
// Check if currently boosting
UFUNCTION(BlueprintCallable, Category = "Boost")
bool IsBoosting() const { return bIsBoosting; }
private:
// Handler for boosting logic
void BoostHandler();
// Handle air boosting mechanics
void AirBoosting();
// Ends the air boost animation/state
UFUNCTION()
void EndAirBoostAnimation();
// Determines if boosting is allowed
bool IsAllowedToBoost() const;
// Pointer to the owning Sonic character
UPROPERTY()
ASonicCharacter* SonicCharacter;
// Timer handle for air boost animation
FTimerHandle AirBoostAnimationTimer;
// Boost state flags
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Boost", meta = (AllowPrivateAccess = "true"))
bool bIsBoosting;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Boost", meta = (AllowPrivateAccess = "true"))
bool bHasAirBoosted;
};
BoostComponent.cpp
cpp
Key Improvements and Explanations
GENERATED_BODY() Placement: Moved to the top of the class body to comply with Unreal Engine standards, ensuring proper code generation.
Forward Declaration: Replaced ASonicCharacterComponent with ASonicCharacter assuming that ASonicCharacter is your character class. Adjust this based on your actual class hierarchy.
Pointer Safety:
Added checks to ensure SonicCharacter is valid before performing operations.
Logged warnings if SonicCharacter is not properly cast, aiding in debugging.
Property Enhancements:
Exposed BoostExtraSpeed, MinimumBoostSpeed, and BoostDuration as UPROPERTY with EditAnywhere to allow easy adjustments via Blueprints or the editor.
Organized properties under appropriate access specifiers (public, protected, private) for better encapsulation.
Boost Logic Enhancements:
Boost Duration: Implemented a timer to automatically release the boost after a set duration, preventing indefinite boosting.
Boosting Event: Broadcasted BoostingEvent every tick while boosting, allowing Blueprints to perform continuous actions like energy consumption or visual effects.
Air Boosting: Ensured that air boosting can only be performed once unless re-enabled, preventing abuse.
Movement Handling:
Applied boost by adding to the character's velocity based on the missing speed to cap at maximum speed, ensuring consistent boost behavior.
Temporarily increased MaxWalkSpeed during boost to allow higher movement speeds, then reset upon boost release.
State Management:
Managed character states (AirBoosting, Falling) appropriately to reflect boosting actions.
Ensured that states are reset correctly after boost animations conclude.
Code Organization:
Separated boost initiation (BoostPressed) from boost termination (), allowing for clear control flows.
Final Notes
Ensure that your ASonicCharacter class has the necessary functions and state management to support these boost mechanics. Specifically, methods like SetCharacterState and properties like GetCharacterState should be implemented to manage character states effectively.
Additionally, always test the boost mechanics thoroughly in various scenarios (e.g., boosting on ground vs. in air, edge cases where speed is just below or above thresholds) to ensure a smooth and bug-free experience.
Feel free to adjust the boost parameters (BoostPower, BoostExtraSpeed, etc.) to better fit the desired gameplay feel for your Sonic remake.
Dataset releases
Not yet included in a published release.
#include "BoostComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Sonic/SonicCharacter.h" // Ensure this is the correct path to your SonicCharacter class
#include "TimerManager.h"
// Sets default values for this component's properties
UBoostComponent::UBoostComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame.
PrimaryComponentTick.bCanEverTick = true;
// Initialize default values
BoostPower = 100.0f;
BoostExtraSpeed = 2000.0f;
MinimumBoostSpeed = 500.0f;
BoostDuration = 2.0f; // Example duration
bIsBoosting = false;
bHasAirBoosted = false;
}
// Called when the game starts
void UBoostComponent::BeginPlay()
{
Super::BeginPlay();
// Attempt to get the owning character
SonicCharacter = Cast<ASonicCharacter>(GetOwner());
if (!SonicCharacter)
{
UE_LOG(LogTemp, Warning, TEXT("UBoostComponent::BeginPlay - Owner is not a ASonicCharacter"));
}
}
// Called every frame
void UBoostComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
if (bIsBoosting)
{
BoostHandler();
// Broadcast the boosting event every tick
BoostingEvent.Broadcast();
}
}
void UBoostComponent::BoostPressed()
{
if (!SonicCharacter)
{
UE_LOG(LogTemp, Warning, TEXT("UBoostComponent::BoostPressed - SonicCharacter is null"));
return;
}
// Prevent boosting if already boosting or air boosted
if (bIsBoosting || bHasAirBoosted)
return;
// Check if character is falling to decide between ground boost and air boost
if (SonicCharacter->GetCharacterMovement()->IsFalling())
{
AirBoosting();
return;
}
// Check if boosting is allowed
if (!IsAllowedToBoost())
return;
// Broadcast boost event to trigger sounds, particles, etc.
OnBoostEvent.Broadcast();
bIsBoosting = true;
// Calculate the missing speed and apply boost
float CurrentSpeed = SonicCharacter->GetCharacterMovement()->Velocity.Size();
float MaxSpeed = SonicCharacter->GetCharacterMovement()->GetMaxSpeed();
float MissingSpeed = FMath::Max(MaxSpeed - CurrentSpeed, 0.0f);
FVector BoostDirection = SonicCharacter->GetActorForwardVector() * (MissingSpeed + BoostPower);
SonicCharacter->GetCharacterMovement()->Velocity += BoostDirection;
// Increase MaxWalkSpeed temporarily
SonicCharacter->GetCharacterMovement()->MaxWalkSpeed += BoostExtraSpeed;
// Optional: Implement a timer to limit the boost duration
GetWorld()->GetTimerManager().SetTimer(AirBoostAnimationTimer, this, &UBoostComponent::BoostReleased, BoostDuration, false);
}
void UBoostComponent::BoostHandler()
{
if (!SonicCharacter)
return;
if (!IsAllowedToBoost())
{
BoostReleased(); // Force release boost if not allowed anymore
return;
}
// Optionally, apply continuous boost effects here
// For example, applying additional movement input
SonicCharacter->AddMovementInput(SonicCharacter->GetActorForwardVector(), BoostPower * GetWorld()->GetDeltaSeconds());
}
void UBoostComponent::AirBoosting()
{
if (!SonicCharacter)
return;
// Check if air boosting is already used
if (bHasAirBoosted)
return;
bHasAirBoosted = true;
// Broadcast air boost event
OnAirBoostEvent.Broadcast();
// Apply air boost
float CurrentSpeed = SonicCharacter->GetCharacterMovement()->Velocity.Size();
float MaxSpeed = SonicCharacter->GetCharacterMovement()->GetMaxSpeed();
float MissingSpeed = FMath::Max(MaxSpeed - CurrentSpeed, 0.0f);
FVector BoostDirection = SonicCharacter->GetActorForwardVector() * (MissingSpeed + BoostPower);
SonicCharacter->GetCharacterMovement()->Velocity += BoostDirection;
// Optionally, adjust MaxWalkSpeed or other movement properties if needed
SonicCharacter->GetCharacterMovement()->MaxWalkSpeed += BoostExtraSpeed;
// Set character state to AirBoosting
SonicCharacter->SetCharacterState(EFEeSonicCharacterState::AirBoosting);
// Set a timer to end the air boost animation/state after a short duration
GetWorld()->GetTimerManager().SetTimer(AirBoostAnimationTimer, this, &UBoostComponent::EndAirBoostAnimation, 0.3f, false);
}
void UBoostComponent::EndAirBoostAnimation()
{
if (!SonicCharacter)
return;
if (SonicCharacter->GetCharacterState() == EFEeSonicCharacterState::AirBoosting)
{
SonicCharacter->SetCharacterState(EFEeSonicCharacterState::Falling);
}
}
bool UBoostComponent::IsAllowedToBoost() const
{
if (!SonicCharacter)
return false;
// Check if character is not falling
bool bIsOnGround = !SonicCharacter->GetCharacterMovement()->IsFalling();
// Check if current speed exceeds the minimum boost speed
bool bHasMinSpeed = SonicCharacter->GetCharacterMovement()->Velocity.Size() > MinimumBoostSpeed;
return bIsOnGround && bHasMinSpeed;
}
void UBoostComponent::BoostReleased()
{
if (!bIsBoosting)
return;
bIsBoosting = false;
if (!SonicCharacter)
return;
// Reset MaxWalkSpeed to its original value
SonicCharacter->GetCharacterMovement()->MaxWalkSpeed = SonicCharacter->GetCharacterMovement()->GetMaxSpeed();
// Broadcast stop boost event
OnStopBoostEvent.Broadcast();
// Clear the boost timer if it's still active
GetWorld()->GetTimerManager().ClearTimer(AirBoostAnimationTimer);
}
void UBoostComponent::EnableAirBoosting()
{
bHasAirBoosted = false;
}
void UBoostComponent::DisableAirBoosting()
{
bHasAirBoosted = true;
}
BoostReleased
Encapsulated helper functions like BoostHandler and AirBoosting to maintain clean and readable code.
Debugging Aids:
Included UE_LOG statements to help identify issues during runtime, especially if the component isn't correctly attached or the cast fails.
Potential Extensions:
Boost Meter: You can integrate a boost meter by adding a CurrentBoost variable that decreases while boosting and regenerates when not.
Visual and Audio Feedback: Link the delegate events (OnBoostEvent, OnAirBoostEvent, etc.) to Blueprint functions to handle particle effects, sounds, and camera shakes.
Energy Consumption: Implement energy or stamina systems that deplete during boosts and regenerate over time.