2017-02-24 5 views
0

안녕하세요 여러분, 저는 Kinect Movmement와 Csharp 언어를 기반으로하는 축구 게임을하고 있습니다.제한하는 방법 3D지도에서 커서처럼 작동하는 버튼 Unity3D

나는 UI 버튼이 있는데 경기장 인 3D 모델에서 볼 수있는 캔버스 캔버스 아래 원으로 만듭니다.

버튼은 커서와 같은 기능을하며, 키 넥트를 앞으로 향한 플레이어의 오른손과 원래의 커서 인 커서 사이에서 동기화하는 스크립트로 번역합니다.

괜찮아요 .2 차원 인 다른 장면에서 3D로 움직일 때 단 한가지 좋은 점이 있습니다.이 위치에서 커서가 움직이는 것을 발견했습니다. -12125.34 , -3132.932 , -1, 전례에 있었지만 당신이 나를 이해하지 않는 경우에 당신이 KINECT 앞으로 당신에게 오른손을 이동하고 같은 손으로 물체를 이동 생각 -616 , 288 , -1

이 위치에 2D 인 장면 내가 그 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 Movement와 동 기적으로 움직이는 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);