assume sonic unleashed / sonic generations 3d movement system.
can you finish and improve this boost component, btw the soniccharactercomponent is the main component and inside of unreal the main character inherents from that component
// Fill out your copyright notice in the Description page of Project Settings.
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
Assistant
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
};
// 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();
}
Certainly! Let's work on finishing and improving your UBoostComponent for your Sonic game in Unreal Engine. I'll incorporate necessary features such as a boost meter, proper state handling, and add comments to explain each part. I'll also ensure that the code aligns with the Sonic Unleashed/Sonic Generations 3D movement systems.
Below is the improved code with explanations:
BoostComponent.h
cpp
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "BoostComponent.generated.h"
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class SONICUNLEASHEDREMAKE_API UBoostComponent : public UActorComponent
{
GENERATED_BODY()
public:
// Constructor
UBoostComponent();
protected:
// BeginPlay override
virtual void BeginPlay() override;
public:
// TickComponent override
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
// Boost properties
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Boost")
float BoostPower;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Boost")
float BoostEnergy; // Current boost energy
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Boost")
float MaxBoostEnergy; // Maximum boost energy
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Boost")
float BoostConsumptionRate; // Rate at which boost energy is consumed
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Boost")
float BoostRegenerationRate; // Rate at which boost energy regenerates when not boosting
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Boost")
float MinBoostSpeed; // Minimum speed required to start boosting
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Boost")
float BoostExtraSpeedConstant; // Additional speed added during boost
// Functions to handle boosting
UFUNCTION(BlueprintCallable, Category = "Boost")
void BoostPressed(); // Called when boost is pressed
UFUNCTION(BlueprintCallable, Category = "Boost")
void BoostReleased(); // Called when boost is released
void HandleBoosting(float DeltaTime); // Handles boosting logic each tick
void AirBoost(); // Handles air boosting
UFUNCTION(BlueprintCallable, Category = "Boost")
void EndAirBoostAnimation(); // Ends the air boost animation
UFUNCTION(BlueprintCallable, Category = "Boost")
bool CanBoost() const; // Checks if boosting is allowed
UFUNCTION(BlueprintCallable, Category = "Boost")
void EnableAirBoosting(); // Enables air boosting
UFUNCTION(BlueprintCallable, Category = "Boost")
void DisableAirBoosting(); // Disables air boosting
UFUNCTION(BlueprintCallable, Category = "Boost")
bool IsBoosting() const { return bIsBoosting; } // Checks if currently boosting
// Event delegates
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnBoostEvent);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnAirBoostEvent);
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FBoostingEvent); // Called every tick while boosting
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnStopBoostEvent);
// Blueprint events
UPROPERTY(BlueprintAssignable, Category = "Boost")
FOnBoostEvent OnBoostEvent;
UPROPERTY(BlueprintAssignable, Category = "Boost")
FOnAirBoostEvent OnAirBoostEvent;
UPROPERTY(BlueprintAssignable, Category = "Boost")
FOnStopBoostEvent OnStopBoostEvent;
UPROPERTY(BlueprintAssignable, Category = "Boost")
FBoostingEvent BoostingEvent;
private:
// Pointer to the character
class ASonicCharacterComponent* SonicCharacter;
// Timer handle for air boost animation
FTimerHandle AirBoostAnimationTimer;
// Flags
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Boost", meta = (AllowPrivateAccess = "true"))
bool bIsBoosting;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Boost", meta = (AllowPrivateAccess = "true"))
bool bHasAirBoosted; // Ensures air boost occurs only once per jump
};
BoostComponent.cpp
cpp
Explanations and Improvements
Boost Meter Implementation:
Added BoostEnergy, MaxBoostEnergy, BoostConsumptionRate, and BoostRegenerationRate to handle the boost meter.
Boost energy decreases when boosting and regenerates over time when not boosting.
Checks ensure the character cannot boost without sufficient energy.
Boost Conditions:
Modified CanBoost() to check whether the character is allowed to boost based on energy, current state, and speed.
Ground boosting requires the character to be on the ground and moving at or above a minimum speed (MinBoostSpeed).
Air boosting is allowed if the character is in the air and hasn't already air-boosted.
Boost Handling Logic:
HandleBoosting() now takes DeltaTime to properly consume boost energy over time.
The boost effect is applied every tick while boosting.
If boost energy runs out, BoostReleased() is called to stop boosting.
Speed Management:
Adjusted how the character's speed is modified during boost to prevent issues like negative speeds.
Upon releasing boost, the character's max walk speed resets to default.
Air Boosting Logic:
AirBoost() consumes a fixed amount of boost energy when performed.
Ensures air boost can only be done once per jump unless reset.
Delegates and Events:
Added BoostingEvent which is broadcasted every tick during boost. This can be used in Blueprints for continuous effects (e.g., particle effects).
Existing events (OnBoostEvent, OnAirBoostEvent, OnStopBoostEvent) are used to trigger sounds, animations, or effects when boost starts or stops.
Code Cleanup and Comments:
Added comments throughout the code for clarity.
Replaced magic numbers with variables for better configurability.
Ensured that the code adheres to Unreal Engine's best practices.
Error Handling:
Added checks to ensure is valid before using it.
How to Use the Improved BoostComponent
Setup in Blueprint: Assign the BoostComponent to your character in the editor. Use the exposed properties to adjust boost behavior.
Input Bindings: Bind your input events (like pressing a boost button) to call BoostPressed() and BoostReleased().
Events Handling: In your character's Blueprint, you can handle OnBoostEvent, BoostingEvent, OnAirBoostEvent, and OnStopBoostEvent to trigger animations, effects, or sounds.
Adjusting Parameters: Tweak BoostConsumptionRate, BoostRegenerationRate, MinBoostSpeed, and BoostExtraSpeedConstant to fine-tune the boost experience.
Additional Considerations
Boost Energy Pickups: Implement logic to increase BoostEnergy when the character collects boost energy items.
UI Feedback: Update your UI to display the boost meter (BoostEnergy relative to MaxBoostEnergy) so players know how much boost they have.
Testing: Thoroughly test the boost in various scenarios to ensure smooth gameplay and adjust parameters as needed.
Conclusion
The improved UBoostComponent now includes all the essential features for a Sonic-like boost mechanic, complete with energy management and proper state handling. By integrating this component into your character and adjusting the parameters, you should achieve a boost system similar to that in Sonic Unleashed or Sonic Generations.
Let me know if you need further assistance or additional features!
Dataset releases
Not yet included in a published release.
#include "BoostComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Kismet/GameplayStatics.h"
#include "SonicCharacterComponent.h"
// Constructor
UBoostComponent::UBoostComponent()
{
PrimaryComponentTick.bCanEverTick = true;
// Initialize default values
BoostPower = 100.0f;
BoostExtraSpeedConstant = 2000.0f;
bIsBoosting = false;
bHasAirBoosted = false;
MaxBoostEnergy = 100.0f;
BoostEnergy = MaxBoostEnergy;
BoostConsumptionRate = 20.0f; // Units of boost energy consumed per second
BoostRegenerationRate = 10.0f; // Units of boost energy regenerated per second when not boosting
MinBoostSpeed = 500.0f; // Minimum speed required to start boosting
}
// BeginPlay
void UBoostComponent::BeginPlay()
{
Super::BeginPlay();
// Get the owning character
SonicCharacter = Cast<ASonicCharacterComponent>(GetOwner());
if (!SonicCharacter)
{
UE_LOG(LogTemp, Error, TEXT("BoostComponent: Owner is not a SonicCharacterComponent."));
}
}
// TickComponent
void UBoostComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// Handle boosting
HandleBoosting(DeltaTime);
// Regenerate boost energy when not boosting
if (!bIsBoosting && BoostEnergy < MaxBoostEnergy)
{
BoostEnergy += BoostRegenerationRate * DeltaTime;
BoostEnergy = FMath::Clamp(BoostEnergy, 0.0f, MaxBoostEnergy);
}
}
// BoostPressed
void UBoostComponent::BoostPressed()
{
// Check if we can boost
if (bIsBoosting || bHasAirBoosted || !CanBoost())
{
return;
}
// Handle air boosting
if (SonicCharacter->GetCharacterMovement()->IsFalling())
{
AirBoost();
return;
}
// Start boosting
OnBoostEvent.Broadcast();
bIsBoosting = true;
// Increase character speed
float CurrentSpeed = SonicCharacter->GetCharacterMovement()->Velocity.Size();
float SpeedDifference = SonicCharacter->GetCharacterMovement()->GetMaxSpeed() - CurrentSpeed;
FVector BoostDirection = SonicCharacter->GetActorForwardVector() * SpeedDifference;
SonicCharacter->GetCharacterMovement()->Velocity += BoostDirection;
SonicCharacter->GetCharacterMovement()->MaxWalkSpeed += BoostExtraSpeedConstant;
// Stop sliding if applicable
if (SonicCharacter->bIsSliding)
{
SonicCharacter->StopSlide();
}
}
// HandleBoosting
void UBoostComponent::HandleBoosting(float DeltaTime)
{
if (!bIsBoosting)
{
return;
}
// Check if we can continue boosting
if (!CanBoost())
{
BoostReleased();
return;
}
// Consume boost energy
BoostEnergy -= BoostConsumptionRate * DeltaTime;
BoostEnergy = FMath::Clamp(BoostEnergy, 0.0f, MaxBoostEnergy);
if (BoostEnergy <= 0.0f)
{
BoostReleased();
return;
}
// Apply forward movement input
SonicCharacter->AddMovementInput(SonicCharacter->GetActorForwardVector(), 2.0f);
// Broadcast boosting event
BoostingEvent.Broadcast();
}
// AirBoost
void UBoostComponent::AirBoost()
{
bHasAirBoosted = true;
OnAirBoostEvent.Broadcast();
// Consume boost energy for air boost
BoostEnergy -= BoostConsumptionRate;
BoostEnergy = FMath::Clamp(BoostEnergy, 0.0f, MaxBoostEnergy);
// Increase character speed
float CurrentSpeed = SonicCharacter->GetCharacterMovement()->Velocity.Size();
float SpeedDifference = SonicCharacter->GetCharacterMovement()->GetMaxSpeed() - CurrentSpeed;
FVector BoostDirection = SonicCharacter->GetActorForwardVector() * SpeedDifference;
SonicCharacter->GetCharacterMovement()->Velocity += BoostDirection;
// Set character state to air boosting
SonicCharacter->CurrentState = EFEeSonicCharacterState::AirBoosting;
// Set timer to end air boost animation
GetWorld()->GetTimerManager().SetTimer(AirBoostAnimationTimer, this, &UBoostComponent::EndAirBoostAnimation, 0.3f, false);
// Disable other air abilities
SonicCharacter->HomingAttackComponent->AirDashDisable();
// Update character state if necessary
if (SonicCharacter->CurrentState == EFEeSonicCharacterState::Jumping)
{
SonicCharacter->CurrentState = EFEeSonicCharacterState::Falling;
}
}
// EndAirBoostAnimation
void UBoostComponent::EndAirBoostAnimation()
{
if (SonicCharacter->CurrentState == EFEeSonicCharacterState::AirBoosting)
{
SonicCharacter->CurrentState = EFEeSonicCharacterState::Falling;
}
}
// CanBoost
bool UBoostComponent::CanBoost() const
{
// Check if we have enough boost energy
if (BoostEnergy <= 0.0f)
{
return false;
}
// Check movement conditions
bool bIsInAir = SonicCharacter->GetCharacterMovement()->IsFalling();
float CurrentSpeed = SonicCharacter->GetCharacterMovement()->Velocity.Size();
// Ground boost conditions
if (!bIsInAir && CurrentSpeed >= MinBoostSpeed)
{
return true;
}
// Air boost conditions
if (bIsInAir && !bHasAirBoosted)
{
return true;
}
return false;
}
// BoostReleased
void UBoostComponent::BoostReleased()
{
if (!bIsBoosting)
{
return;
}
bIsBoosting = false;
// Reset character's max walk speed
float DefaultMaxSpeed = SonicCharacter->GetCharacterMovement()->GetMaxSpeed();
SonicCharacter->GetCharacterMovement()->MaxWalkSpeed = DefaultMaxSpeed;
OnStopBoostEvent.Broadcast();
}
// EnableAirBoosting
void UBoostComponent::EnableAirBoosting()
{
bHasAirBoosted = false;
}
// DisableAirBoosting
void UBoostComponent::DisableAirBoosting()
{
bHasAirBoosted = true;
}
SonicCharacter
Used FMath::Clamp() to ensure variables stay within valid ranges.