"Creating a balanced unicycle: How to animate a rider on it

I’m Creating a Unicycle Which can be Control Using A and D And you have to balance it I created a script for this But its Just not working I’ve implemented basic input handling and attempted to adjust the unicycle’s balance accordingly, but it’s not yielding the desired results. The unicycle either behaves erratically or fails to respond accurately to player input, making the gameplay experience less enjoyable than intended.

It just falls Apart
this is the script i wrote
`

include “unicycle.h”
include “PhysicsEngine/PhysicsConstraintComponent.h”
include “Components/SkeletalMeshComponent.h”

// Sets default values
Aunicycle::Aunicycle()
{
PrimaryActorTick.bCanEverTick = true;

UnicycleMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("UnicycleMesh"));
RootComponent = UnicycleMesh;

// Enable physics simulation
UnicycleMesh->SetSimulatePhysics(true);

// Ensure the collision is enabled for physics interactions
UnicycleMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);

MaxSpeed = 600.f; // Adjust as necessary

}

// Called when the game starts or when spawned
void Aunicycle::BeginPlay()
{
Super::BeginPlay();
}

// Called every frame
void Aunicycle::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
ApplyBalancingForce();
}

// Called to bind functionality to input
void Aunicycle::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis(“MoveForward”, this, &Aunicycle::MoveForward);
}

void Aunicycle::MoveForward(float Value)
{
const FVector Force = FVector(Value * MaxSpeed, 0.f, 0.f); // Adjust force magnitude as necessary
UnicycleMesh->AddForce(Force); // Apply force to the skeletal mesh
}

void Aunicycle::ApplyBalancingForce()
{
// Get the current rotation and angular velocity
const FRotator Rotation = UnicycleMesh->GetComponentRotation();
const FVector AngularVelocity = UnicycleMesh->GetPhysicsAngularVelocityInDegrees();

// Constants for balancing adjustments - these would need tuning based on testing
const float Stability = 0.5f;
const float Speed = 0.3f;   

// Calculate corrective torque
const float RollTorque = -Stability * Rotation.Roll - Speed * AngularVelocity.Y;
const float PitchTorque = -Stability * Rotation.Pitch - Speed * AngularVelocity.X;

// Apply a corrective torque to stabilize the unicycle
UnicycleMesh->AddTorqueInDegrees(FVector(PitchTorque, RollTorque, 0.f));

}
`

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms