2013-10-04 75 views
1

我一直致力於Unity3D中的3D移動應用程序,但最近遇到了問題。我有一個操縱桿,可以讓我的角色走路。操縱桿是一種gui紋理。我設置了邊界,因此無法在屏幕上移動操縱桿。它的設置使得當你的手指移動到邊界之外時,操縱桿將盡可能遠地移動,當你將手指移回時,手指會繼續移動。這很好,除了我要設置它以便沿着屏幕右側移動另一個手指來旋轉相機。當一個手指放在操縱桿上,並將另一個手指放在屏幕的右側時,會發生該問題。當我沿着屏幕右側移動一個手指時,操縱桿被重置!這是我的代碼...請幫助!如何停止二手手指在Unity3D中操縱gui操縱桿的位置

#pragma strict 
var gui: GUITexture; 
var pixelInsetPosResSet: boolean = true; 
var playerCharacter: GameObject; 
public var leftID: int; 

function Start() 
{ 
guiPixelInsetResSet(); 
gui.color.a=.1; 
gui.transform.position.z=1; 

} 

function JoystickGUIMove() 
{ 
// Detecting Touch 
if(Input.touchCount > 0){ 

    for(var i : int = 0; i < Input.touchCount; i++){ 

    var touch : Touch = Input.GetTouch(i); 

    if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position)) 
    { 
     leftID = Input.GetTouch(i).fingerId; 
     pixelInsetPosResSet = false; 
    } 
if(pixelInsetPosResSet == false) 
{ 
if(Input.GetTouch(i).fingerId == leftID) 
{ 
// Moving GUI and Character 
    gui.pixelInset.x = touch.position.x+Screen.width/(-1.78); 
    gui.pixelInset.y = touch.position.y+Screen.height/(-1.63); 
    gui.color.a=.5; 
    if(gui.pixelInset.y > Screen.height/(-3.5)) 
    { 
     playerCharacter.transform.position.z = playerCharacter.transform.position.z+(8*Time.deltaTime); 
    } 
    else if(gui.pixelInset.y > Screen.height/(-2.6)) 
    { 
     playerCharacter.transform.position.z = playerCharacter.transform.position.z+(2*Time.deltaTime); 
    } 
    else if(gui.pixelInset.y < Screen.height*(-.475)) 
    { 
     playerCharacter.transform.position.z = playerCharacter.transform.position.z-(2*Time.deltaTime); 
    } 
    if(gui.pixelInset.x > Screen.width/(-2.4)) 
    { 
     playerCharacter.transform.position.x = playerCharacter.transform.position.x+(2*Time.deltaTime); 
    } 
    if(gui.pixelInset.x > Screen.width/(-2.4) && gui.pixelInset.y > Screen.height/(-3.5)) 
    { 
     playerCharacter.transform.position.x = playerCharacter.transform.position.x+(5*Time.deltaTime); 
    } 
    if(gui.pixelInset.x < Screen.width/(-2.1)) 
    { 
     playerCharacter.transform.position.x = playerCharacter.transform.position.x-(2*Time.deltaTime); 
    } 
    if(gui.pixelInset.x < Screen.width/(-2.1) && gui.pixelInset.y > Screen.height/(-3.5)) 
    { 
     playerCharacter.transform.position.x = playerCharacter.transform.position.x-(5*Time.deltaTime); 
    } 
    //Setting boundaries 
    if(pixelInsetPosResSet == false) 
    { 
     if((touch.position.y+Screen.height/(-1.63)) > Screen.height/(-4.5)) 
     { 
     gui.pixelInset.y = Screen.height/(-4.5); 
     } 
     if((touch.position.y+Screen.height/(-1.63)) < Screen.height*(-.5)) 
     { 
     gui.pixelInset.y = Screen.height*(-.5); 
     } 
     if((touch.position.x+Screen.width/(-1.78)) > Screen.width/(-2.6)) 
     { 
     gui.pixelInset.x = Screen.width/(-2.6); 
     } 
     if((touch.position.x+Screen.width/(-1.78)) < Screen.width*(-.5)) 
     { 
     gui.pixelInset.x = Screen.width*(-.5); 
     } 
     } 
     else 
     { 
      pixelInsetPosResSet = true; 
      pixelInsetPositionReset(); 
     } 
     } 
     } 


    pixelInsetPositionReset(); 

} 
} 
} 

function pixelInsetPositionReset() 
{ 
// Reseting Joystick position 
for(var i : int = 0; i < Input.touchCount; i++){ 

var touch : Touch = Input.GetTouch(i); 

if(touch.phase == TouchPhase.Ended) 
{ 
    pixelInsetPosResSet = true; 
    guiPixelInsetResSet(); 
} 
} 
} 

function guiPixelInsetResSet() 
{ 
// Set width and height automatically 
if (pixelInsetPosResSet == true) 
{ 
gui.pixelInset.width=Screen.width/8.5; 
gui.pixelInset.height=gui.pixelInset.width; 
// Set position automatically 
gui.pixelInset.x=Screen.width/(-2.25); 
gui.pixelInset.y=Screen.height/(-2.35); 
} 
} 

function Update() 
{ 
pixelInsetPositionReset(); 
gui.color.a=.1; 
JoystickGUIMove(); 
} 

回答

1

簡單的解決方案是存儲觸摸你的左邊fingerId一個變量說leftId當它擊中GUITexture,做同樣的右fingerId說rightId。現在當你檢查左手柄上的觸摸時,檢查Input.touches[i].fingerId != rightId和右手柄的類似檢查Input.touches[i].fingerId != leftId

我也遇到過這個問題,並用它解決了它。

+0

謝謝!我還無法測試,但我會盡快得到機會! –

+0

好的確定..請問有沒有問題。聊天室的Unity3d http://chat.stackoverflow.com/rooms/37624/unity3d-developers – Nick

+0

仍然有一些問題...編輯的問題。如果你能幫上忙,那就太好了:) –

1

好的,我修好了!我不得不使用touch.fingerId而不是Input.GetTouch(i).fingerId 花了一段時間,但現在它已經開始運行! :)

+0

哦,終於你明白了。很好..在這裏很好,邏輯爲你工作。如果有任何疑問,現在您可以加入Unity3d開發人員的聊天室。 – Nick