2017-02-24 61 views
0

您好大家我工作的一个足球游戏,这是基于Kinect的Movmement和CSHARP语言编写的。如何限制按钮在3D地图状光标工作Unity3D

我有一个UI按钮和I使它像在画布 循环,画布3D模型是在体育场中。

按钮就像是一个光标,并通过脚本,这是我们前进的Kinect和光标这是原来的按钮播放器的右手之间synchronises翻译。

好吧所有这些限制工作良好和完美,只是一件事,当我从其他场景是2D到这个场景是3D我发现光标在这个位置移动-12125.34 , -3132.932 , -1, 它是在先例场景是2D在这个位置 -616 , 288 , -1

,如果你不明白我只是觉得你前进,你的右手Kinect和你用同一只手移动对象 我写为istriction的代码是

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 

public class HandController : MonoBehaviour { 

    public static HandController Instance; 

    private Vector2 initialPosition;//body initial position 

    public GameObject cursorObject; 
    private bool cursorEnter = false; 

    private float delay ; 
    private float timeing; 
    private float firstContactTime; 
    private string[] flagsTable = new string [] { 
     "Tunisia", "Maroco","Austria", "Belgium", "cameroon", 
     "cote_d_ivoire", "Egypt","france", "guini", "jordan", 
     "liberia", "madagascar","mali", "mauritius", "moldova", 
     "romanie", "Slovakie","Spain", "United_kingdom", "Vanuatu", 
     "polgne" 
    }; 


    private bool checkingFlagsTableDone = false; 
    public static bool flagsFound = false; 
    public static string countryChoosingName = ""; 
    public Button btnSelected; 
    public string buttonSelected = ""; 
    public Button myCursor; 
    public Image myCursorImage; 

    void Awake() 
    { 
     Instance = this; 

    } 
    void Start() { 

     DontDestroyOnLoad(transform.root.gameObject); 
     // var vertExtent = Camera.main.camera.orthographicSize; 
     //var horzExtent = vertExtent * Screen.width/Screen.height; 

    } 
    public int offstX= 937; 
    public int offstY= 520; 

    bool isInited=false; 

    void Update() { 
     float initialXposition = transform.localPosition.x; 
     float initialYposition = transform.localPosition.y; 
     float nextXposition=0; 
     float nextYposition=0; 

     KinectManager manager = KinectManager.Instance; 

     if (DepthImageViewer.Instance.jointColliders != null) 
     { 
      if (manager.Player1Calibrated && isInited == false) 
      { 
       isInited = true; 
       nextXposition = transform.localPosition.x; 
       nextYposition = transform.localPosition.y; 
       if (initialXposition != nextXposition && initialYposition != nextYposition) 
       { 
        // Debug.Log("I moved the cursor the the initial pisition"); 
       } 
       //else 
        // Debug.Log("Sorry I didn't translate the cursor"); 
      } else if (manager.Player1Calibrated && isInited == true) 
      { 
       float XvalueAre = DepthImageViewer.Instance.jointColliders[11].transform.localPosition.x * 192.0f + 184430.4f + offstX; 
       float YvalueAre = DepthImageViewer.Instance.jointColliders[11].transform.localPosition.y *108.0f + 58832.1f + offstY; 


      Vector2 newPos = new Vector2(XvalueAre - offstX, YvalueAre - offstY); 
       transform.position = new Vector3(newPos.x,newPos.y,-1); 
      } 
     } 
     else { Debug.Log("Erreur remplir joints colliders "); } 
     /////////////////////////////////////////////Delay Time Over Button ///////////////////////////////////////////// 


     if (cursorEnter == true) { 
      delay = Time.time - firstContactTime; 
      //Debug.Log("waiting time is => " + delay); 
      myCursorImage .fillAmount = 1 - (delay/3) ; 
      if (delay > 3) { 

       if (currentBtn != null) { 
        CheckOnOverButtonName(currentBtn.name); 

        Debug.Log("Button Name => " + currentBtn.name); 
        if (flagsFound == true) 
        { 
         Debug.Log("the country choose is => " + countryChoosingName); 
         currentBtn.onClick.Invoke(); 
         myCursorImage.fillAmount = 1; 
         cursorEnter = false; 
         buttonSelected = countryChoosingName; 
        } 
        Debug.Log("the country choose is => " + currentBtn.name); 
        currentBtn.onClick.Invoke(); 
        myCursorImage.fillAmount = 1; 
        cursorEnter=false; 
       } 
      } 
     } 

    } 

    Button currentBtn; 
    void OnTriggerEnter2D(Collider2D coll) 
    { 
     firstContactTime = Time.time; 
     cursorEnter = true; 
     currentBtn = coll.GetComponent<Button>(); 
    } 

    void OnTriggerExit2D(Collider2D other) 
    { 
     cursorEnter = false; 
     flagsFound = false; 
     countryChoosingName = ""; 
     checkingFlagsTableDone = false; 
    } 


    void CheckOnOverButtonName(string buttonName) 
    { 
     for(int i = 0 ; i < flagsTable.Length ; i++){ 
      if (string.Compare(flagsTable[i],buttonName) == 0) { 
       flagsFound = true; 
       countryChoosingName = flagsTable[i]; 
       if (i == flagsTable.Length) { 
        checkingFlagsTableDone = true; 
       } 
      } 
     } 
    } 
} 
+0

喜后,我编辑你写的相当严重的文本,使其更有点清楚,但我仍然无法理解这是怎么回事?你有在2d中同步移动到Kinect运动的UI按钮?另外,“限制”是什么意思? – Maakep

+0

我假设在2D中使用了与深度无关的具有相同大小的拼写相机。当切换到3D时,您必须考虑远离摄像机的位置偏差较大。 –

回答

0

你好我解决售后服务问题使用Mathf.Clamp

float XvalueAre = DepthImageViewer.Instance.jointColliders[11].transform.localPosition.x * 192.0f + 184430.4f + offstX; 
        float YvalueAre = DepthImageViewer.Instance.jointColliders[11].transform.localPosition.y * 108.0f + 58832.1f + offstY; 

        float x = Mathf.Clamp(XvalueAre, 0.0f,1024.0f); 
        float y = Mathf.Clamp(YvalueAre, 0.0f,768.0f); 
        transform.position = new Vector3(x, y, -1);