如何使用 C# 交换 "rigid body" 与 8 方向的加力?

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

我是 C# unity 的初学者。我在 8 direction 中搜索用 add force 交换立方体的解决方案,但我找不到任何东西。我真的很困惑,我该怎么做。有人请在这方面指导我。任何解决问题的代码示例或教程将不胜感激。

提前致谢。 这是我的代码:

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 ();
    }
}

好的,试试这个,如果它适合你。

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;  
        }
    }
}