簡體   English   中英

如何使用C#在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;  
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM