# Android Swipe In Unity 3D World with AR [closed]

I am working on an AR application using Unity3D and the Vuforia SDK for Android. The way the application works is a when the designated image(a frame marker in our case) is recognized by the camera, a 3D island is rendered at that spot. Currently I am able to detect when/which objects are touched on the model by raycasting. I also am able to successfully detect a swipe using this code:

if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];

switch (touch.phase)
{
case TouchPhase.Began:

couldBeSwipe = true;

startPos = touch.position;

startTime = Time.time;

break;

case TouchPhase.Moved:

if (Mathf.Abs(touch.position.y - startPos.y) > comfortZoneY)
{

couldBeSwipe = false;

}
//track points here for raycast if it is swipe
break;

case TouchPhase.Stationary:

couldBeSwipe = false;

break;

case TouchPhase.Ended:

float swipeTime = Time.time - startTime;

float swipeDist = (touch.position - startPos).magnitude;

if (couldBeSwipe && (swipeTime < maxSwipeTime) && (swipeDist > minSwipeDist))
{

// It's a swiiiiiiiiiiiipe!

float swipeDirection = Mathf.Sign(touch.position.y - startPos.y);

// Do something here in reaction to the swipe.
swipeCounter.IncrementCounter();
}

break;

}

touchInfo.SetTouchInfo
(Time.time-startTime,(touch.position-startPos).magnitude,Mathf.Abs (touch.position.y-startPos.y));
}


Thanks to andeeeee for the logic. But I want to have some interaction in the 3D world based on the swipe on the screen. I.E. If the user swipes over unoccluded enemies, they die. My first thought was to track all the points in the moved TouchPhase, and then if it is a swipe raycast into all those points and kill any enemy that is hit. Is there a better way to do this? What is the best approach?

Thanks for the help!

• This question is too broad. The "best" approach is quite subjective and really depends on your game and what mechanic you're aiming for. Also: The code you posted has nothing to do with the question really, removing it would probably improve/clarify your question. – bummzack Nov 7 '13 at 8:36

Well it depends on how you want to kill enemies. If you want to kill anything you touched during swipe then you should either do a raycast in each frame whci you are swiping and record all hits in a list and then kill them all when swipe is approved or do an sphereCast from start position to end position. something like this 1- At start do a raycast and find the point p1 on ground 2- at end do another raycast and find the point p2 on ground 3- Do a sphere cast from p1 to p2. This is possible if you don't have covers which you want to block enemies between cover and your camera from being killed.

The swipe code is a little unfriendly as well cause a little stantionary should be ok if you don't want only really fast swipes and also you could do it using Input.GetMouseButtonX calls to make it possible to test it easily in editor and make it cross platform.