Download

Compare Input vector from stick and compare with actor rotation vector

Hi guys,

I’m currently making a a simple ledge grab system, however i’ve run into a problem.

The character can grab ledges at any angle, for example along a curve of a cylinder. I have the character grabbing the ledge at the correct position but i’m trying to get basic climb and drop behaviour, so if the player pushes in the direction (or approximate direction, maybe using a tolerance) then the character will climb up, if they push away from the ledge they will drop and if they push to the extreme left or right of the ledge direction nothing happens. Please see the picture attached.

I currently have an FVector of the direction the ledge is facing and i What i need to do is get the direction of the stick input and compare that with the direction of the ledge grab trigger box (this box is a trigger volume that checks for the character and snaps the character to the ledge with the correct orientation and location) Note that the trigger always faces in the opposite direction for the ledge. See the picture below.

I currently have this, but i find that at some angles the character will climb, or drop when they shouldn’t, for example holding the stick directly away from the ledge.


// If the direction is within a tolerance of the ledge grab direction climb up
    if (FMath::IsNearlyEqual((float)xVal, (float)CurrentLedgegrabDirection.X, (float)CharacterStatsData->LedgeClimbDirectionToleranceX) || FMath::IsNearlyEqual((float)yVal, (float)CurrentLedgegrabDirection.Y, (float)CharacterStatsData->LedgeClimbDirectionToleranceY))
    {
        ClimbFromLedgeGrab();
    }
    else
    {
        DropFromLedgeGrab(CharacterStatsData->LedgeDropVector);
    }

The code above takes the value of the input from the stick and then checks if it is within a tolerance of the X or Y axis of the ledge direction… Z is not needed. I may well be doing this the total wrong way and i’ve been scratching my head about this for ages, I’m fairly new to vector math and have read about the dot product to get angles between vectors, but have tried with odd results.

Any help would be appreciated.

Bump… cany anyone point me in the direction of some help then? I’ve researched this and read a fair few posts and tutorials, but have not been successful. Even if someone has a rough idea on the best way to implement that, it would be great.

In case anyone is interested i managed to get this working, heres a code snippet:


// Get the stick movement direction
    FVector md = (GetActorForwardVector() * StickXValue + GetActorRightVector() * StickYValue);
    // Rotate the ledge grab vector to match the character direction
    FVector RotLgv = GetActorRotation().RotateVector(CurrentLedgegrabDirection);

    //GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, FString::Printf(TEXT("MD: %s"), *md.ToString()));
    //GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, FString::Printf(TEXT("ROT LGV: %s"), *RotLgv.ToString()));
    // Compare the two vectors to see if the user is pressing roughly in the direction of the ledge
    if (md.Equals(RotLgv,1.25))
    {
        //GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, "CLIMB");
        ClimbFromLedgeGrab();
    }
    else
    {
        //GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, "CANT CLIMB");
        DropFromLedgeGrab(CharacterStatsData->LedgeDropVector);
    }

I’m basically getting the ledge grab direction from the trigger and rotating that vector to match the character rotation as the character is always facing the ledge grab tirgger and therefore facing in the exact opposite way of the ledge. Next i am caching the stick input value from the movement functions and using that to get the exact value of the stick movement direction. I now compare these two vectors with a tolerance to allow the player to be pushing in the general direction of the ledge and allow the character to climb up.