# Unity 2D detect if swipe goes through an object [closed]

I am developing a game in unity with flying arrows that the user tries to deflect by swiping through them (changing their direction). I can get the arrows to move with the swipes, but they move no matter where the swipes occur. I want them to only move (be affected) iff the swipe goes through the arrow. Here's what I have so far:

public class swipe : MonoBehaviour
{

private Vector2 leftFingerPos = Vector2.zero;
private Vector2 leftFingerLastPos = Vector2.zero;
private Vector2 leftFingerMovedBy = Vector2.zero;

public float slideMagnitudeX = 0.0f;
public float slideMagnitudeY = 0.0f;

bool touched = false;

void Update()
{
if (Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);

if (touch.phase == TouchPhase.Began)
{
leftFingerPos = Vector2.zero;
leftFingerLastPos = Vector2.zero;
leftFingerMovedBy = Vector2.zero;

slideMagnitudeX = 0;
slideMagnitudeY = 0;

// record start position
leftFingerPos = touch.position;

}

else if (touch.phase == TouchPhase.Moved)
{
leftFingerMovedBy = touch.position - leftFingerPos; // or Touch.deltaPosition : Vector2
// The position delta since last change.
leftFingerLastPos = leftFingerPos;
leftFingerPos = touch.position;

// slide horz
slideMagnitudeX = leftFingerMovedBy.x / Screen.width;

// slide vert
slideMagnitudeY = leftFingerMovedBy.y / Screen.height;

if(touched)
{
//rigidbody2D.velocity = new Vector2(slideMagnitudeX, slideMagnitudeY)*20;
touched = false;
}
}

else if (touch.phase == TouchPhase.Stationary)
{
leftFingerLastPos = leftFingerPos;
leftFingerPos = touch.position;

slideMagnitudeX = 0.0f;
slideMagnitudeY = 0.0f;
}

else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
slideMagnitudeX = 0.0f;
slideMagnitudeY = 0.0f;
}
}

}

void OnMouseEnter()
{
touched = true;
}
}


The only way I have been able to find so far is using "onMouseEnter" which only works for very slow swipes.

How do I detect whether a swipe goes through an object?

## closed as off-topic by Anko, Josh♦Apr 20 '15 at 15:23

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Josh
If this question can be reworded to fit the rules in the help center, please edit the question.

• Your title is not a Google search query. It is the title to a question. Please fix it. – Nic Hartley Jan 12 '15 at 4:01

You have both the current position and the last position on move. (touch.position, leftFingerPos)

You can do a ray cast using these to see if there is a collision with your arrow.

http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html