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 · Jun 17, 2012 at 10:45 AM · positionjoystick

Joystick position

Hi, can someone please tell me why when I use the below code the joystick is at the far right side of the screen in all aspects but when compiled it is just off centre.

 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() {
 
     gui = GetComponent("GUITexture"); //Gather the reference for the GUITexture component
 
         gui.pixelInset.x = Screen.width - gui.pixelInset.width - 30;
         gui.pixelInset.y = Screen.height - Screen.height + 30;
         
         Debug.Log(gui.pixelInset.x + "  " + gui.pixelInset.y);
 
 
     //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
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Virtual Joystick Active follow the finger position??? 0 Answers

AngryBots mobile joysticks position 0 Answers

can i find Joystick Position based on screen size? 0 Answers

Use Gamepad right analog stick instead of mouse to control crosshair movement 3 Answers

How do I access a gamepad's joystick position? 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