Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7701247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:56:20+00:00 2026-05-31T22:56:20+00:00

This is my problem: I have a player class and SwipeDetector Class in c#,

  • 0

This is my problem: I have a player class and SwipeDetector Class in c#, the SwipeDetector class helps to do recognized swipe touches vertically on the iPhone.

p.s. I’m using unity3d but this is a programming question oppose to gaming techniques :))

In my player class I’m trying to access the SwipeDetector and find out which is swipe it was (up, down).

player.cs:

if(SwipeDetetcor is up){
   print("up");
}

this is the SwipeDetector class, it looks scary but its not!!

    using UnityEngine;
    using System.Collections;

    public class SwipeDetector : MonoBehaviour {

        // Values to set:
        public float comfortZone = 70.0f;
        public float minSwipeDist = 14.0f;
        public float maxSwipeTime = 0.5f;

        private float startTime;
        private Vector2 startPos;
        private bool couldBeSwipe;

        public enum SwipeDirection {
            None,
            Up,
            Down
        }

        public SwipeDirection lastSwipe = SwipeDetector.SwipeDirection.None;
        public float lastSwipeTime;

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

                switch (touch.phase)
                {
                    case TouchPhase.Began:
                        lastSwipe = SwipeDetector.SwipeDirection.None;
                                            lastSwipeTime = 0;
                        couldBeSwipe = true;
                        startPos = touch.position;
                        startTime = Time.time;
                        break;

                    case TouchPhase.Moved:
                        if (Mathf.Abs(touch.position.x - startPos.x) > comfortZone)
                        {
                            Debug.Log("Not a swipe. Swipe strayed " + (int)Mathf.Abs(touch.position.x - startPos.x) +
                                      "px which is " + (int)(Mathf.Abs(touch.position.x - startPos.x) - comfortZone) +
                                      "px outside the comfort zone.");
                            couldBeSwipe = false;
                        }
                        break;
                    case TouchPhase.Ended:
                        if (couldBeSwipe)
                        {
                            float swipeTime = Time.time - startTime;
                            float swipeDist = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;

                            if ((swipeTime < maxSwipeTime) && (swipeDist > minSwipeDist))
                            {
                                // It's a swiiiiiiiiiiiipe!
                                float swipeValue = Mathf.Sign(touch.position.y - startPos.y);

                                // If the swipe direction is positive, it was an upward swipe.
                                // If the swipe direction is negative, it was a downward swipe.
                                if (swipeValue > 0){
                                    lastSwipe = SwipeDetector.SwipeDirection.Up;
                                    print("UPUPUP");
                                }
                                else if (swipeValue < 0)
                                    lastSwipe = SwipeDetector.SwipeDirection.Down;

                                // Set the time the last swipe occured, useful for other scripts to check:
                                lastSwipeTime = Time.time;
                                Debug.Log("Found a swipe!  Direction: " + lastSwipe);
                            }
                        }
                        break;
                }
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:56:21+00:00Added an answer on May 31, 2026 at 10:56 pm

    If you want to access your SwipeDetector from the player class, you simply can use a public variable.

    // Player.cs
    public SwipeDetector MySwipeDetector;
    
    void Update() 
    {
        if (MySwipeDetector.lastSwipe == SwipeDirection.Up) { .... }
    }
    

    If you don’t want to set the public variable in unity, you can use a kind of singletone pattern.

    // SwipeDetector.cs
    private static SwipeDetector _Instance;
    
    public static SwipeDetector Instance { get { return _Instance; } }
    
    void Awake()
    {
        if (_Instance!= null) throw new Exception(...);
        _Instance= this;
    }
    

    And use it this way:

    // Player.cs
    void Update()
    {
        if (SwipeDetector.Instance.lastSwipe == SwipeDirection.Up) { .... }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: I have solved the majority of this problem but have run into a
I am not sure how to approach this problem: 'Player' class maintains a list
I'm building a game, and for my players I have a player class. This
Okey, this is my problem. I have one service class where Ive managed to
Consider this problem: I have a program which should fetch (let's say) 100 records
I am wondering how you would approach this problem I have two Taxrates that
I am trying to learn ASP.NET MVC and I hit this problem: I have
Ok I have this problem that I've never had before, it's really bugging me.
Ok I have this problem I'm trying to use Jquery to load a partial
So I have this problem with strings and switch-case, and I'll try to keep

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.