Download

Input problems

Hi guys, I try to create a character which is follows the camera direction, but you can pan around it when you stop moving.
When i try to move left/right the player still moving forward/backward, and the player stops following the camera direction if you turn to fast,pass a certain point or sometimes not even working.
If I hold down the aim button everything just works fine, i can move every direction and the character keeps following the camera. I can’t figure it out, maybe you guys can help me.
Thanks

The code
.h



public:
    /** Lean and others */
    float LeanAngleInterp;
    void ProceduralLeaning();
    void CycleMovement();
    FVector CalculateWorldDirection();


.cpp



ASCharacter::ASCharacter(const class FObjectInitializer& ObjectInitializer)
    /* Override the movement class from the base class to our own to support multiple speeds (eg. sprinting) */
    : Super(ObjectInitializer.SetDefaultSubobjectClass<USCharacterMovementComponent>(ACharacter::CharacterMovementComponentName))
{
    /...
    UCharacterMovementComponent* MoveComp = GetCharacterMovement();
    // Adjust jump to make it less floaty
    MoveComp->JumpZVelocity = 800;
    MoveComp->AirControl = 0.7f;
    MoveComp->GravityScale = 2.0f;
    MoveComp->MaxAcceleration = 1500.0f;
    MoveComp->BrakingFrictionFactor = 0.0f;
    MoveComp->MaxWalkSpeedCrouched = 200;
    MoveComp->bCanWalkOffLedgesWhenCrouching = true;
    MoveComp->RotationRate = FRotator(0, 0, 340.0f);
    MoveComp->bOrientRotationToMovement = true;

    // For the camera rotation
    bUseControllerRotationYaw = false;
    /...
}

/...
void ASCharacter::SetTargeting(bool NewTargeting)
{
    bIsTargeting = NewTargeting;
    bUseControllerRotationYaw = NewTargeting;

    if (Role < ROLE_Authority)
    {
        ServerSetTargeting(NewTargeting);
    }
}

/...
void ASCharacter::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    CycleMovement();
}

/...
void ASCharacter::MoveForward(float Val)
{
    if (Controller && Val != 0.f)
    {
        /*
        const FRotator Rotation = Controller->GetControlRotation();
        const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::X);
        */

        if (IsTargeting())
        {
            const FVector Forward = GetActorForwardVector();
            AddMovementInput(Forward, Val);
        }
        else
        {
            const FVector Direction = CalculateWorldDirection();
            AddMovementInput(Direction, Val);
        }
    }
}

void ASCharacter::MoveRight(float Val)
{
    if (Val != 0.f)
    {
        /*
        const FRotator Rotation = GetActorRotation();
        const FVector Direction = FRotationMatrix(Rotation).GetScaledAxis(EAxis::Y);
        */

        if (IsTargeting())
        {
            const FVector Right = GetActorRightVector();
            AddMovementInput(Right, Val);
        }
        else
        {
            const FVector Direction = CalculateWorldDirection();
            AddMovementInput(Direction, Val);
        }
    }
}

/...
void ASCharacter::CycleMovement()
{
    FVector ControlVector = GetControlRotation().Vector();
    FVector Projected = FVector::VectorPlaneProject(ControlVector, GetActorUpVector()).GetSafeNormal();
    FRotator NormalRot = Projected.Rotation();

    FVector Forward = NormalRot.Vector() * GetInputAxisValue(FName("MoveForward"));
    FVector Right = UKismetMathLibrary::GetRightVector(NormalRot) * GetInputAxisValue(FName("MoveRight"));

    NormalDirection = (Forward + Right).GetSafeNormal();
}

FVector ASCharacter::CalculateWorldDirection()
{
    UCharacterMovementComponent* MoveComp = GetCharacterMovement();

    float CharRotation = MoveComp->RotationRate.Yaw * GetWorld()->DeltaTimeSeconds;
    float DotNormal = FVector::DotProduct(GetActorForwardVector(), NormalDirection);

    float NormalDegree = UKismetMathLibrary::DegAcos(DotNormal);
    float b2 = CharRotation / NormalDegree;
    int clamped = UKismetMathLibrary::FClamp(b2, -1.0, 1.0);

    FRotator RotFromX = NormalDirection.Rotation();
    FRotator FinalRotation = UKismetMathLibrary::RLerp(GetActorRotation(), RotFromX, clamped, true);

    return FinalRotation.Vector();
}


Nvm, solved it.



FVector ASCharacter::CalculateWorldDirection()
{
    UCharacterMovementComponent* MoveComp = GetCharacterMovement();

    return UKismetMathLibrary::RLerp( GetActorRotation(), NormalDirection.Rotation(), UKismetMathLibrary::FClamp( (MoveComp->RotationRate.Yaw * GetWorld()->DeltaTimeSeconds) / UKismetMathLibrary::DegAcos(FVector::DotProduct(GetActorForwardVector(), NormalDirection)), -1.0f, 1.0f), true).Vector();
}


Its working but I don’t know why it dosen’t work when I use local variables… Any tips from anyone who can explain it to me? :'D