Custom movement component for Sonic character
/
UCLASS()
class SONICUNLEASHEDREMAKE_API USonicMovementComponent : public UCharacterMovementComponent
{
GENERATED_BODY()
// Generations stomp will stop all movement and move you down
// Unleashed stomp will keep your current momentum so send you down and still move you forward
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stomp", meta = (ClampMin = "0"))
bool UnleashedStomp;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Stomp", meta = (ClampMin = "0"))
float StompPower;
};
// Fill out your copyright notice in the Description page of Project Settings.
//Calculate the lateral movement scale based on current speed, this will limit the influence of lateral input.
float USonicMovementComponent::CalculateLateralMovementScale(float Speed) const
{
float MaxSpeed = MaxWalkSpeed;
float Scale = 1.0f - (Speed / MaxSpeed);
return FMath::Clamp(Scale, MinLateralMovementScale, 1.0f);
}
if (Speed > 0.1f)
{
// Calculate the target rotation based on velocity
FRotator TargetRotation = CurrentVelocity.Rotation();
// Calculate the turn radius based on speed
float SpeedRatio = FMath::Clamp(Speed / MinSpeedForMaxTurnRadius, 0.0f, 1.0f);
float CurrentTurnRadius = FMath::Lerp(MinTurnRadius, MaxTurnRadius, SpeedRatio);
// Adjust the rotation rate based on the turn radius
float CurrentRotationRate = 360.0f / (2 * PI * CurrentTurnRadius);
RotationRate = FRotator(0.0f, CurrentRotationRate, 0.0f);
// Interpolate the current rotation to the target rotation
FRotator NewRotation = FMath::RInterpTo(SonicCharacter->GetActorRotation(), TargetRotation, DeltaTime, RotationInterpSpeed);
// Set the new rotation
SonicCharacter->SetActorRotation(NewRotation);
}
}
void USonicMovementComponent::GoOutOfControl(float Duration)
{
if (Duration <= 0.0f) return;
Right now sonic can only run on the floor, but i want it so, once you run fast enough, (around 500 units) you can start running on walls and if you maintain your speed you can keep running on the walls, or even roofs.
but i only want this to happen if it happens smoothly, simply running and jumping against a wall should NOT work, only if the ground allows for smooth movement all around.
So if the ground allows for a smooth way to go from the ground to the wall and then wall to roof in a smooth manner, then it should work. The camera should always stay upright.
also i want sonics mesh to correctly rotate based on if hes running on the wall and if hes on sloped floors ect, can you think you can properly implement this. if so, do it
Yes, I can help you implement this functionality. Below is the modified code to enable Sonic to run on walls and ceilings when he reaches a certain speed and only if the transition is smooth. The camera will remain upright, and Sonic's mesh will rotate correctly based on the surface he's running on.
USonicMovementComponent.h
First, we'll need to define custom movement modes and add necessary variables and functions to handle wall running and ceiling running.
cpp
// Add this enum to define custom movement modes
UENUM(BlueprintType)
enum class ECustomMovementMode : uint8
{
CMOVE_None UMETA(DisplayName = "None"),
CMOVE_WallRunning UMETA(DisplayName = "WallRunning"),
CMOVE_CeilingRunning UMETA(DisplayName = "CeilingRunning"),
};
// In your SonicMovementComponent class
UCLASS()
class SONICUNLEASHEDREMAKE_API USonicMovementComponent : public UCharacterMovementComponent
{
GENERATED_BODY()
public:
USonicMovementComponent();
// Existing functions...
protected:
// Existing protected members...
// Override PhysCustom to handle custom movement modes
virtual void PhysCustom(float deltaTime, int32 Iterations) override;
// Functions to handle custom movement modes
void PhysWallRunning(float deltaTime, int32 Iterations);
void PhysCeilingRunning(float deltaTime, int32 Iterations);
// Functions to manage wall running state
void CheckForWallRunning();
void StartWallRunning(const FVector& WallNormal);
void StopWallRunning();
// Functions to manage ceiling running state
void CheckForCeilingRunning();
void StartCeilingRunning(const FVector& CeilingNormal);
void StopCeilingRunning();
// Helper functions
bool IsSurfaceWalkable(const FVector& SurfaceNormal) const;
bool ShouldBeWallRunning() const;
// Variables for wall running
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "WallRunning", meta = (ClampMin = "0"))
float MaxWallRunSpeed;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "WallRunning", meta = (ClampMin = "0"))
float MinSpeedToWallRun;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "WallRunning", meta = (ClampMin = "0"))
float WallRunGravityScale;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "WallRunning", meta = (ClampMin = "0"))
float WallRunDetectionDistance;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "WallRunning", meta = (ClampMin = "0"))
float MaxWallRunAngle;
UPROPERTY()
FVector CurrentWallNormal;
// Variables for camera
UPROPERTY()
bool bMaintainCameraUpright;
};
USonicMovementComponent.cpp
Now, we'll implement the functions declared above.
cpp
Adjusting the Camera
To keep the camera upright while Sonic is running on walls or ceilings, we'll need to override the camera behavior.
In your Sonic character class (e.g., ASonicCharacterComponent), adjust the camera settings:
cpp
void ASonicCharacterComponent::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// Existing input bindings...
// Disable pawn control rotation
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
// Ensure the camera stays upright
if (CameraBoom)
{
CameraBoom->bUsePawnControlRotation = false;
}
}
In your tick function or after updating the character's rotation, adjust the camera to remain upright:
cpp
void ASonicCharacterComponent::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// Keep the camera upright
if (Controller)
{
FRotator ControlRotation = Controller->GetControlRotation();
ControlRotation.Roll = 0.0f;
Controller->SetControlRotation(ControlRotation);
}
}
Adjusting the Mesh Rotation
To ensure Sonic's mesh rotates correctly based on the surface, adjust the mesh's rotation after the movement component updates:
cpp
void ASonicCharacterComponent::PostNetReceiveVelocity(const FVector& NewVelocity)
{
Super::PostNetReceiveVelocity(NewVelocity);
// Align mesh with the floor or wall
FVector UpVector = GetActorUpVector();
FRotator MeshRotation = FRotationMatrix::MakeFromZX(UpVector, GetActorForwardVector()).Rotator();
GetMesh()->SetWorldRotation(MeshRotation);
}
Final Notes
Smooth Transitions: The code checks the angle between the current surface normal and the next surface normal to ensure transitions only happen when the angle is less than MaxWallRunAngle (e.g., 45 degrees). This ensures that Sonic can only start wall running on smooth transitions.
Speed Check: Sonic must be moving faster than MinSpeedToWallRun (e.g., 500 units) to start wall running.
Camera Orientation: The camera remains upright by adjusting its rotation after the character's rotation is updated.
Mesh Rotation: Sonic's mesh is rotated to align with the surface normal, so he appears correctly oriented on walls and ceilings.
Movement Input: Movement input is projected onto the plane of the wall or ceiling so that controls remain intuitive.
Gravity: The gravity is adjusted during wall running to keep Sonic attached to the wall. When wall running ends, gravity is reset.
This implementation should provide the functionality you're looking for. Remember that fine-tuning may be necessary to get the exact feel you want, and testing in the Unreal Engine editor will help you make any necessary adjustments.