Download

C++ Ramp Character Movement Issue

Hi,

Ive implemented a custom movement component based on PhysFlying to use to make a sonic style movement where i rotate the character to match the surface.

This seems to work with 1 player (on 120fps). However when i test it on lower performance (~30fps - 60fps) the movement and rotation seems to be disturbed by collisions… (So the character really slows down once it goes into the ramp)

As example this image:

Below the custom flying movement code. No is implemented just testing the ramp movement first.




void UMyMovementComponent::PhysCustom(float deltaTime, int32 Iterations)
{


    if (deltaTime < MIN_TICK_TIME)
    {
        return;
    }

    RestorePreAdditiveRootMotionVelocity();

    if (!HasAnimRootMotion() && !CurrentRootMotion.HasOverrideVelocity())
    {
        if (bCheatFlying && Acceleration.IsZero())
        {
            Velocity = FVector::ZeroVector;
        }
        const float Friction = 0.5f * GetPhysicsVolume()->FluidFriction;
        CalcVelocity(deltaTime, Friction, true, GetMaxBrakingDeceleration());
    }

    ApplyRootMotionToVelocity(deltaTime);

    Iterations++;
    bJustTeleported = false;

    FVector OldLocation = UpdatedComponent->GetComponentLocation();
    const FVector Adjusted = Velocity * deltaTime;
    FHitResult Hit(1.f);


    // getting rotation using linetrace
    FRotator rotation;
    bool SurfaceHit = GetSurfaceRotation(rotation);

    if (SurfaceHit) {
        SafeMoveUpdatedComponent(Adjusted, rotation, true, Hit);
    }



    // ?
    if (Hit.Time < 1.f)
    {
        const FVector GravDir = FVector(0.f, 0.f, -1.f);
        const FVector VelDir = Velocity.GetSafeNormal();
        const float UpDown = GravDir | VelDir;

        bool bSteppedUp = false;
        if ((FMath::Abs(Hit.ImpactNormal.Z) < 0.2f) && (UpDown < 0.5f) && (UpDown > -0.2f) && CanStepUp(Hit))
        {
            float stepZ = UpdatedComponent->GetComponentLocation().Z;
            bSteppedUp = StepUp(GravDir, Adjusted * (1.f - Hit.Time), Hit);
            if (bSteppedUp)
            {
                OldLocation.Z = UpdatedComponent->GetComponentLocation().Z + (OldLocation.Z - stepZ);
            }
        }

        if (!bSteppedUp)
        {
            HandleImpact(Hit, deltaTime, Adjusted); // does nothing?
            SlideAlongSurface(Adjusted, (1.f - Hit.Time), Hit.Normal, Hit, true);
        }
    }

    if (!bJustTeleported && !HasAnimRootMotion() && !CurrentRootMotion.HasOverrideVelocity())
    {
        Velocity = (UpdatedComponent->GetComponentLocation() - OldLocation) / deltaTime;
    }


}