x


Joystick, touch

Hi, I'm using a standard joystick script but seem to be having some problems with it, while using it in iOS on the iPhone4 In landscape I can touch anywhere above centre screen and move around just as if I was using the joystick but more jerky .. anyone know why please ?

#pragma strict

var radius : float; //Radius of movement
var deadZoneRadius : float; //Radius of dead zone within which the input value returned is 0

private var gui : GUITexture; //Reference to the GUITexture component of this gameObject
private var startingPosition : Vector2; //Starting position of this gui
private var startingScreenCenter : Vector2; //Starting center screen position of this gui
private var touchIndex : int = -1; //Index of the iPhone touch that hits this gui

private var otherJoysticks : Joystick[];

function Awake() {

 var childGUITextures;
 gui = GetComponent("GUITexture"); //Gather the reference for the GUITexture component
 if(gui.pixelInset.x > 240) { //If joystick is on the right-half of screen, move it to the right-edge if necessary
 var difference : int = 0;
 difference = Screen.width - gui.pixelInset.x - 120;

 childGUITextures = GetComponentsInChildren(typeof(GUITexture));
 for (var childGUI : GUITexture in childGUITextures) {
 childGUI.pixelInset.x += difference;
 }
 }

 //Calculate starting position and starting screen center
 startingPosition = Vector2(gui.pixelInset.x, gui.pixelInset.y);
 startingScreenCenter = Vector2(gui.pixelInset.x + gui.pixelInset.width * 0.5, gui.pixelInset.y + gui.pixelInset.height * 0.5);

 otherJoysticks = FindObjectsOfType(Joystick); //Gather references to all joysticks in scene
}

function Update () {
 var touch : Touch;
 if(touchIndex == -1) { //If a touch has not hit this gui since last time a touch was ended/canceled
 for(var i=0; i<Input.touchCount; i++) { //Go through all current touches
 touch = Input.GetTouch(i); //Get touch
 if(gui.HitTest(touch.position)) { //If this touch hits this gui
 touchIndex = i; //Set touch index to this touch
 for(var j=0; j<otherJoysticks.Length; j++) {
 if(otherJoysticks[j] != this)
 otherJoysticks[j].TouchTaken(touchIndex);
 }
 break; //We've found our touch so break out of the loop
 }
 }
 }
 if(touchIndex != -1 && touchIndex < Input.touchCount) { //If a touch has hit this gui and its index does not point to an invalid element in the current touch array
 touch = Input.GetTouch(touchIndex); //Get touch
 if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) { //If touch phase is Began or Moved
 var difference : Vector2 = touch.position - startingScreenCenter; //Calculate the difference between current touch position and the starting center screen position of this gui
 if(difference.magnitude > radius) { //If the length of the difference vector is greater than the radius of movement
 //Limit the distance of the difference vector so that input stays within the [-1..1] range
 var radians = Mathf.Atan2(difference.y, difference.x);
 difference.x = Mathf.Cos(radians) * radius;
 difference.y = Mathf.Sin(radians) * radius;
 }
 //Set the pixel inset of the GUITexture to show the movement of the joystick
 gui.pixelInset.x = difference.x + startingPosition.x;
 gui.pixelInset.y = difference.y + startingPosition.y;
 } else if(touch.phase == TouchPhase.Canceled || touch.phase == TouchPhase.Ended) { //If touch phase is Ended or Canceled
 touchIndex = -1; //Reset touch index to an invalid number
 //Reset the pixel inset of the GUITexture to its starting position
 gui.pixelInset.x = startingPosition.x;
 gui.pixelInset.y = startingPosition.y;
 }
 } else {
 touchIndex = -1; //Reset touch index to an invalid number
 //Reset the pixel inset of the GUITexture to its starting position
 gui.pixelInset.x = startingPosition.x;
 gui.pixelInset.y = startingPosition.y;
 }
}

//Returns the current input axis vector
function GetAxis() {
 var axis = Vector3(gui.pixelInset.x, gui.pixelInset.y, 0) - startingPosition;
 return (axis.magnitude < deadZoneRadius) ? Vector3.zero : axis/radius;
}

//Returns whether a given touch position is contained within this gui
function ContainsPosition(touchPosition : Vector2) {
 return gui.HitTest(touchPosition);
}

//Called by other Joysticks when a touch has been taken
function TouchTaken(takenTouchIndex : int) {
 if(touchIndex == takenTouchIndex) { //If the touch that another Joystick took has the same index as this Joystick's touch
 touchIndex = -1; //Let the touch go
 //Reset the pixel inset of the GUITexture to its starting position
 gui.pixelInset.x = startingPosition.x;
 gui.pixelInset.y = startingPosition.y;
 }
}
more ▼

asked Aug 08 '12 at 05:32 AM

Griffo gravatar image

Griffo
1.7k 20 48 66

function BotherToReadScript(codeIsFormatted:boolean){if(!codeIsFormatted){return null;}}

Aug 08 '12 at 06:33 AM gregzo

Can you say that in English please ..

Aug 08 '12 at 07:02 AM Griffo

If you format your code, people might consider reading it.

Aug 08 '12 at 09:10 AM gregzo

Well, why didn't you say that instead of being sarcastic ..

Aug 08 '12 at 12:05 PM Griffo
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x577
x214

asked: Aug 08 '12 at 05:32 AM

Seen: 438 times

Last Updated: Aug 08 '12 at 12:17 PM