我一直在使用Unity3D中的3D移动应用程序,但最近遇到了一个问题。我有一个操纵杆让我的角色走路。操纵杆是一种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 ();
}
答案 0 :(得分:1)
对此的简单解决方案是在触及GUITexture时将左手fingerId触摸存储到变量说leftId
,并为右手指IDI rightId
执行相同操作。现在,当您检查左操纵杆中的触摸时,检查Input.touches[i].fingerId != rightId
和右侧操纵杆的类似情况,检查Input.touches[i].fingerId != leftId
。
我也遇到过这个问题并用它来解决它。
答案 1 :(得分:1)
好的,我修好了!我不得不使用touch.fingerId
代替Input.GetTouch(i).fingerId
花了一段时间,但现在它已经运行了! :)