简体   繁体   中英

How to Swap “rigid body” with add force in 8 direction using C#?

I am beginner of C# unity. I was searching the solution to swap cube with add force in 8 direction , but I could not find anything. I am really confused that how can I do this. Someone please guide me in this regard. Any code example or tutorial for solving the problem will be appreciated.

Thanks in advance. Here is my code:

using UnityEngine;
using System.Collections;

public class Swaping1 : MonoBehaviour {
    int swipeIndex = -1; //index of first detected swipe to prevent multiple swipes
    public Vector2 startPos;  //starting position of touch
    public float minSwipeDist;

    public float UpSwape;
    public float DownSwape;
    public float LeftSwape;
    public float RightSwape;
    public GUIButtons GUIButtonsObj;

    // Use this for initialization
    void Start () {
        GUIButtonsObj=GetComponent<GUIButtons>();
    }

    public void swipe()
    {
        for (int i = 0; i < Input.touchCount; i++) {
            Touch touch = Input.touches [i];
            switch (touch.phase) {
            case TouchPhase.Began:
                if (swipeIndex == -1) {
                    swipeIndex = i;
                    startPos = touch.position;
                }
                break;
            case TouchPhase.Moved:
                if (i != swipeIndex)
                    break;
                Vector2 direction = touch.position - startPos;
                //vertical swipe
                if (Mathf.Abs (direction.x) < Mathf.Abs (direction.y)) {
                    //swipe up
                    if ((touch.position.y - startPos.y) > minSwipeDist) {
                        //GUIButtonsObj.UpButton();
                        //transform.Translate(0f, UpSwape, 0f);
                        swipeIndex = -1;
                    }
                    //swipe down
                    else if ((touch.position.y - startPos.y) < -minSwipeDist) {
                        //transform.Translate(0f, -DownSwape, 0f);
                        //GUIButtonsObj.DownButton();
                        swipeIndex = -1;
                    }
                }
                //horizontal swipe
                else {
                    //swipe right
                    if ((touch.position.x - startPos.x) < -minSwipeDist) {
                        //transform.Translate(-RightSwape, 0f, 0f);
                        GUIButtonsObj.LeftButton();
                        swipeIndex = -1;
                    }
                    //swipe left
                    else if ((touch.position.x - startPos.x) > minSwipeDist) {
                        //  transform.Translate(LeftSwape, 0f, 0f);
                        GUIButtonsObj.RightButton();
                        swipeIndex = -1;
                    }
                }
                break;
            }
        }
    }

    // Update is called once per frame
    void Update () 
    {
        swipe ();
    }
}

Ok, Try this, if it works for you.

using UnityEngine;

public enum Swipes { None, Up, Down, Left, TopLeft, BottomLeft, Right, TopRight,  BottomRight};

public class SwipeManager : MonoBehaviour
{
    public float minSwipeLength = 200f;
    Vector2 currentSwipe;

    private Vector2 fingerStart;
    private Vector2 fingerEnd;

    public static Swipes direction;

    void Update ()
    {
        SwipeDetection();
    }

    public void SwipeDetection ()
    {
        if (Input.GetMouseButtonDown(0)) {
            fingerStart = Input.mousePosition;
            fingerEnd  = Input.mousePosition;
        }

        if(Input.GetMouseButton(0)) {
            fingerEnd = Input.mousePosition;

            currentSwipe = new Vector2 (fingerEnd.x - fingerStart.x, fingerEnd.y - fingerStart.y);

            // Make sure it was a legit swipe, not a tap
            if (currentSwipe.magnitude < minSwipeLength) {
                direction = Swipes.None;
                return;
            }

            float angle = (Mathf.Atan2(currentSwipe.y, currentSwipe.x) / (Mathf.PI));
            Debug.Log(angle);
            // Swipe up
            if (angle>0.375f && angle<0.625f) {
                direction = Swipes.Up;
                Debug.Log ("Up");
                // Swipe down
            } else if (angle<-0.375f && angle>-0.625f) {
                direction = Swipes.Down;
                Debug.Log ("Down");
                // Swipe left
            } else if (angle<-0.875f || angle>0.875f) {
                direction = Swipes.Left;
                Debug.Log ("Left");
                // Swipe right
            } else if (angle>-0.125f && angle<0.125f) {
                direction = Swipes.Right;
                Debug.Log ("Right");
            }
            else if(angle>0.125f && angle<0.375f){
                direction = Swipes.TopRight;
                Debug.Log ("top right");
            }
            else if(angle>0.625f && angle<0.875f){
                direction = Swipes.TopLeft;
                Debug.Log ("top left");
            }
            else if(angle<-0.125f && angle>-0.375f){
                direction = Swipes.BottomRight;
                Debug.Log ("bottom right");
            }
            else if(angle<-0.625f && angle>-0.875f){
                direction = Swipes.BottomLeft;
                Debug.Log ("bottom left");
            }
        }

        if(Input.GetMouseButtonUp(0)) {
            direction = Swipes.None;  
        }
    }
}

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