how to check if the swipe is in the forward direction of my player

The problem is that I am moving my player based on the swipe direction but I want to move my player when the swipe is in the forward direction of my player. To put it simply

if (swipeDirection == myPlayersForwardDirection)
{
    //then move forward
}
else
{
   //don't move
}

I tried many things but didn’t know how to compare the swipe direction and players forward direction.

and here is my code for the swipe.

//inside class
Vector2 firstPressPos;
Vector2 secondPressPos;
Vector2 currentSwipe;
 
public void Swipe()
{
     if(Input.GetMouseButtonDown(0))
     {
         //save began touch 2d point
        firstPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
     }
     if(Input.GetMouseButtonUp(0))
     {
            //save ended touch 2d point
        secondPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y);
       
            //create vector from the two points
        currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
           
        //normalize the 2d vector
        currentSwipe.Normalize();
    }
}

just tell me that my swipe direction is in the forward direction of my gameobject (and by forward direction i mean transform.forward)

Well a swipe direction is a Vector2 in screen space and transform.forward is a Vector3 in world space so it is impossible for them to match. There is context missing here to answer your question properly.


Is the game 3D or 2D?


I’m imagining you’re working in 2D since you say the forward direction should match the swipe direction. Even in 2D, it will be near impossible to get an exact match, so you can’t do this if(swipeDirection == playerDirection). You will have to set up a max variance and check if you are within that variance. I suggest you use Vector2.Dot(normalizedSwipeDirection, normalizedPlayerXYDirection);. In the code below, I set the acceptableVariance to 0.9f which is quite low and allows a 10% margin of error (18+/- degrees), so you will have to adjust to your satisfaction. Just to clarify, 1.0f is a perfect match, but that is close to impossible, so somewhere between 0.9f - 0.95f is acceptable in my opinion.


var acceptableVariance = 0.9f;
var variance = Vector2.Dot(currentSwipe, Vector2(player.transform.forward.x, player.transform.forward.y).normalized);

if (variance > acceptableVariance)
{
    Debug.Log("The swipe direction is valid to move the player");
}
else
{
    Debug.Log("The swipe direction is NOT valid for player movement");
}

If your game is in 3D (or the player doesn’t only move along the xy axes in world space) you have to give more context, but you will basically have to project your screen space swipe into 3D world space in a logical way which of course can differ case to case as you may be trying to superimpose depth in the swipe for example. Anyways, here is the simplest way to transform your 2D screen space swipe into world space (but this assumes that the player only moves parallel with the camera plane which is restrictive, but the best I can do with the limited info):


var worldSwipeDirection = Camera.main.transform.TransformDirection(swipeDirection).normalized;
var acceptableVariance = 0.9f;
var variance = Vector3.Dot(worldSwipeDirection, transform.forward);

if (variance > acceptableVariance)
{
    Debug.Log("player move swipe");
}
else
{
    Debug.Log("Not a valid swipe for player move");
}