Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
    
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
add comment

closed as too broad by bummzack, Byte56 Nov 8 '13 at 20:27

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.