我一直在 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 ();
}