Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Griffo · Aug 08, 2012 at 05:32 AM · touchjoystick

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;
  }
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gregzo · Aug 08, 2012 at 06:33 AM 0
Share

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

avatar image Griffo · Aug 08, 2012 at 07:02 AM 0
Share

Can you say that in English please ..

avatar image gregzo · Aug 08, 2012 at 09:10 AM 0
Share

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

avatar image Griffo · Aug 08, 2012 at 12:05 PM 0
Share

Well, why didn't you say that ins$$anonymous$$d of being sarcastic ..

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make virtual joystick (Windows Store)? 1 Answer

Joystick Zone + Screen-swipe touch input clash. Solution??? 1 Answer

[HELP] Joystick Movement/Button Movement 0 Answers

How would I add android support to this character controller? 1 Answer

iOS navigation + touch 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges