2013-10-04 3 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

이에 대한 간단한 해결책은 GUITexture 안타 rightId 말을 바로 fingerId에 대해 동일한 작업을 수행 할 때 leftId 말을 변수에 터치의 왼쪽 fingerId를 저장합니다. 이제 왼쪽 조이스틱에서 터치를 확인하면 Input.touches[i].fingerId != rightId과 그와 유사한 조이스틱이 Input.touches[i].fingerId != leftId인지 확인하십시오.

나는 또한이 문제에 직면하여 이것을 사용하여 해결했습니다.

+0

고마워요! 나는 이것을 아직 시험 할 수는 없지만 기회가 생기면 곧 갈 것이다! –

+0

확인. 문제가 있는지 물어보십시오. Unity3d의 대화방 http://chat.stackoverflow.com/rooms/37624/unity3d-developers – Nick

+0

아직도 문제가 있습니다 ... 편집 된 질문. 당신이 위대한 것이 도움이 될 수 있다면 :) –

1

좋아, 해결했습니다! 나는 Input.GetTouch(i).fingerId 대신에 touch.fingerId을 사용해야했다. 동안은 시간이 걸렸다. 그러나 이제 시작되었다. :)

+0

오 마침내 당신은 그것을 얻었다. 대단합니다 .. 논리가 당신을 위해 작동했다는 것을 잘 알고 있습니다. 이제 의심이가는 경우 Unity3d 개발자의 채팅룸에 참여할 수 있습니다. – Nick