简体   繁体   中英

How do I use my mobile input results in another scripts if statement? C# Unity

I'm using this script http://www.thegamecontriver.com/2014/08/unity3d-swipe-input-for-touch-screen.html

which basically detects swipes on my android device.

How do I use this script in my character controller's if statement?

For example:

if(**swipescript detects swipe right** && character is idle)
{
    move character right
}

The part in asterisk is what I need help with.

Assuming that the script you posted link to is named SwipeControl , you can add a public boolean variable called swipedRight . Each time player swipes right, set it to true.

Then in your main script where you are suppose to check for that condition, set it to false when it is true .

In SwipeControl :

    public bool swipedRight = false;
//Check if drag distance is greater than 20% of the screen height
        if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
        {//It's a drag
              //check if the drag is vertical or horizontal 
              if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
              {   //If the horizontal movement is greater than the vertical movement...
                  if ((lp.x>fp.x))  //If the movement was to the right)
                  {   //Right swipe
                      //Set Swiped Right to true
                      swipedRight  = true;
                      Debug.Log("Right Swipe");
                  }
                  else
                  {   //Left swipe
                      Debug.Log("Left Swipe"); 
                  }
              }
            else
            {   //the vertical movement is greater than the horizontal movement
                 if (lp.y>fp.y)  //If the movement was up
                 {   //Up swipe
                     Debug.Log("Up Swipe"); 
                 }
                 else
                 {   //Down swipe
                     Debug.Log("Down Swipe");
                 }
            }
        } 

Then from the other Script:

public CharacterController cController;
Rigidbody ccRigidBody;

SwipeControl swipeControl;

void Start()
{
        //Get reference to SwipeControl script
        swipeControl = GameObject.Find("GameObjectSwipeControlIsAttachedTo").GetComponent<SwipeControl>();

        //Get reference to the Rigidbody in the CharacterController
        ccRigidBody = ccRigidBody.GetComponent<Rigidbody>();
}

void Update()
{
        if (swipeControl.swipedRight && ccRigidBody.velocity==Vector3.zero)
        {
            //Do your stuff here


            //Then set swipedRight to false
            swipeControl.swipedRight = false;
        }else if(swipeControl.swipedRight){
            swipeControl.swipedRight = false;
        }
}

So your if(**swipescript detects swipe right** && character is idle) code is translated to if (swipeControl.swipedRight && ccRigidBody.velocity==Vector3.zero) in the code above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM