Download

restrict player movement

Hey guys first time making a game and looking for some guidance with C++.
The Problem I’m trying to solve is that I do not want to let the majority of the character mesh for main character hang off of the platform and the collision still hold the entire model up. I would prefer that the movement in that be stopped before the player walks off most of the platform and then they be given the option to climb down off of the platform. So I had the Idea of adding a box to the player and positioning the collision box in front of the characters feet and offset below the character slightly that way it is always in front of the player and in the ground. My idea being that while the collision box is overlapping with an object that the player can move but when it is not then the player would not be able to. here is my implementation, please tell me why it is not working,

In the constructor:



IsCollided
CollisionMesh = CreateDefaultSubobject<UBoxComponent>(FName("Collision Mesh"));
CollisionMesh->SetupAttachment(GetRootComponent());;


In the BeginPlay function:



void AfpsCharacter::BeginPlay()
{

    Super::BeginPlay();

    CollisionMesh->OnComponentBeginOverlap.AddDynamic(this, &AfpsCharacter::OnOverlapBegin);
    CollisionMesh->OnComponentEndOverlap.AddDynamic(this, &AfpsCharacter::OnOverlapEnd);

}


The OverlapBegin and OverlapEnd functions




void AfpsCharacter::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    bIsCollided = true;
    UE_LOG(LogTemp, Warning, TEXT("colliding"));
}

void AfpsCharacter::OnOverlapEnd(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
    bIsCollided = false;
    UE_LOG(LogTemp, Warning, TEXT("not colliding"));
}



In the Character Movement functions:



void AfpsCharacter::MoveForward(float Value)
{
    if ((Controller != NULL) && (Value != 0.0f))
    {
        // find out which way is forward
        const FRotator Rotation = Controller->GetControlRotation();
        const FRotator YawRotation(0, Rotation.Yaw, 0);

        // get forward vector
        const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);

        //Checking to see if the CollisionMesh is overlapping with anything
         if (bIsCollided)
        {
            AddMovementInput(Direction, Value);
        }
    }

}

void AfpsCharacter::MoveRight(float Value)
{

        if ((Controller != NULL) && (Value != 0.0f))
        {
            // find out which way is right
            const FRotator Rotation = Controller->GetControlRotation();
            const FRotator YawRotation(0, Rotation.Yaw, 0);

            // get right vector 
            const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
            // add movement in that direction

             //Checking to see if the CollisionMesh is overlapping with anything
              if (bIsCollided)
            {
                AddMovementInput(Direction, Value);
            }
        }


}


The way this reads in my brain is: while the CollisionMesh is overlapping with anything: addMovementInput
else don’t: addMovementInput

when in practice it just takes what ever the initial value is for bIsCollided and allows the character to move if its true and not if its false
please help