• 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 G_Sacristan · Jul 04, 2011 at 08:08 PM · joystickboundarycyclepenelopenightmare

joystick boundary is rectangle not cycle

HI, folks! Ive got problem with distance measurement between two screen positions (Vector2) - joysticks boundary is rectangle like, but it should be like cycle (i thought using distance variable as radius and it would provide cycle like boundary).

PS. this script is based on Penelope tutorial Joystick script

                 var clampedPos: Vector2;
                 clampedPos.x = Mathf.Clamp(touchPos.x,guiBoundary.min.x,guiBoundary.max.x);
                 clampedPos.y = Mathf.Clamp(touchPos.y,guiBoundary.min.y,guiBoundary.max.y);
                 
                 var distance: float=Vector2.Distance(defPos,clampedPos);
                 
                 if(distance<moveDistance)
                 {    
                     
                     gui.pixelInset.x=clampedPos.x;
                     gui.pixelInset.y=clampedPos.y;
                 }

Thanks in advance!

Comment
Add comment · Show 3
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 Waz · Jul 04, 2011 at 09:49 PM 0
Share

That code will position the joystick within a circle of radius moveDistance, so it is not clear what problem you are having.

avatar image G_Sacristan · Jul 04, 2011 at 10:43 PM 0
Share

The problem is that it doesnt somehow!

avatar image G_Sacristan · Jul 04, 2011 at 10:50 PM 0
Share

https://docs.google.com/drawings/d/1ZuzB$$anonymous$$W8Nvpn35eVSXNd-T9RVHy$$anonymous$$cUjmtvONb0sLgBOc/edit?hl=en_US

problem is showed graphicaly

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Bunny83 · Jul 04, 2011 at 11:55 PM

What you need is Vector2.ClampMagnitude. If you clamp the two axis seperated it will always be a rectangle ;)

 touchPos = Vector2.ClampMagnitude(touchPos,moveDistance);



Comment
Add comment · Show 1 · Share
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 Amerging Staar · Jul 14, 2014 at 07:11 AM 0
Share

unable to fit this code :( but i am sure this is right. please tell me where to insert this code snippet in the code of asked question. Thanks in advance

avatar image
1

Answer by Waz · Jul 05, 2011 at 05:34 AM

Ah, given you're picture the problem is clearer - your gui boundary is smaller than you're moveDistance. You need to clip an area inside the rectangle - so the guiboundary needs to be at least moveDistance on its shortest side. Bunny83's answer is the way to code it, once you've checked that the touch is inside the rectangle (otherwise it will move the joystick for a touch anywhere, presumably not what you want).

Comment
Add comment · Share
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
0

Answer by citizen_rafiq · Sep 01, 2013 at 10:47 AM

//you need two round texture one for boundary are which is bigger then 2nd texture which will //be touch and drag for controlling

public class JoyStick : MonoBehaviour {

 public Texture areaTexture;
 public Texture touchTexture;
 public Vector2 joystickPosition = new Vector2( 135f,135f);
 public Vector2 speed = new Vector2(2,100);
 public float zoneRadius=100f;
 public float touchSize = 30;
 public float deadZone=20;
 public float touchSizeCoef=0;
 protected Vector2 joystickAxis;
 protected Vector2 joystickValue;
 public Vector2 joyTouch;
 private Vector2 _joystickCenter;
 [SerializeField]
 private Vector2 _smoothing = new Vector2(20f,20f);
 public Vector2 Smoothing 
 {
     get {
        return this._smoothing;
     }
     set {
        _smoothing = value;
        if (_smoothing.x<0.1f){
          _smoothing.x=0.1f;
        }
        if (_smoothing.y<0.1){
          _smoothing.y=0.1f;   
        }
     }
 }
 private int _joystickIndex=-1;
 private bool _enaReset;
 private bool _enaZoom;
  
 void Start () 
 {
     _joystickCenter = joystickPosition;
     _enaReset=false;
 }
 
 void Update () 
 {
        if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) 
         {
            foreach (Touch touch in Input.touches)
            {
              if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) 
              {
                   if (_joystickIndex==touch.fingerId){
                   _joystickIndex=-1;
                   _enaReset=true;
                   }
              }
      
              if(_joystickIndex==touch.fingerId)
              {
                   OnTouchDown(touch.position);
              }
              if (touch.phase == TouchPhase.Began)
              {
                   if (((Vector2)touch.position - _joystickCenter).sqrMagnitude < Mathf.Pow((zoneRadius+touchSizeCoef/2),2))
                     {
                       _joystickIndex = touch.fingerId;
                   }
              }
        }
  
        UpdateJoystick();
        if(_enaReset)
         {
              ResetJoystick();
            }
     }
     else
     { 
        if (Input.GetButtonUp ("Fire1"))     
         {
              _joystickIndex=-1;
              _enaReset=true;
        }
        if(_joystickIndex==1)
         {
              OnTouchDown(Input.mousePosition);
         }
         if (Input.GetButtonDown ("Fire1") ) 
         {
              if (((Vector2)Input.mousePosition - _joystickCenter).sqrMagnitude <Mathf.Pow( (zoneRadius+touchSizeCoef/2),2))
              {
                   _joystickIndex = 1;
      
              }
      
        }
        if(_enaReset)
         {
              ResetJoystick();
            }
  
        UpdateJoystick();
  
     }
  
 }
  
 
 
 
 private void UpdateJoystick()
     { 
        if (joyTouch.sqrMagnitude>deadZone*deadZone)
         {
  
          joystickAxis = Vector2.zero;
            if (Mathf.Abs(joyTouch.x)> deadZone)
           {
               joystickAxis = new Vector2( (joyTouch.x -(deadZone*Mathf.Sign(joyTouch.x)))/(zoneRadius-touchSizeCoef-deadZone),joystickAxis.y);
  
           }
          else
          {
               joystickAxis = new Vector2( joyTouch.x /(zoneRadius-touchSizeCoef),joystickAxis.y);
  
          }
        if (Mathf.Abs(joyTouch.y)> deadZone)
         {
              joystickAxis = new Vector2( joystickAxis.x,(joyTouch.y-(deadZone*Mathf.Sign(joyTouch.y)))/(zoneRadius-touchSizeCoef-deadZone));
         }
         else{
           joystickAxis = new Vector2( joystickAxis.x,joyTouch.y/(zoneRadius-touchSizeCoef));  
          }
  
        }
        else{
          joystickAxis = new Vector2(0,0);
        }
     Vector2 realvalue = new Vector2(  speed.x*joystickAxis.x,speed.y*joystickAxis.y);
     joystickValue=realvalue;
     print(realvalue);
  
 }
 
 void OnTouchDown(Vector2 position)
     {
        joyTouch  = new Vector2( position.x, position.y) - _joystickCenter;
        if ((joyTouch/(zoneRadius-touchSizeCoef)).sqrMagnitude > 1)
         {
          joyTouch.Normalize();
          joyTouch *= zoneRadius-touchSizeCoef;
        }
     //print(joyTouch);
  }
 
 
 private void ResetJoystick()
 {
     if (joyTouch.sqrMagnitude>0.1)
     {
        joyTouch = new Vector2( joyTouch.x - joyTouch.x*_smoothing.x*Time.deltaTime, joyTouch.y - joyTouch.y*_smoothing.y*Time.deltaTime);    
     }
     else{
        joyTouch = Vector2.zero;
        _enaReset=false;
     }
 }
 void OnGUI()
 {
        GUI.DrawTexture( new Rect(_joystickCenter.x -zoneRadius ,Screen.height- _joystickCenter.y-zoneRadius,zoneRadius*2,zoneRadius*2), areaTexture,ScaleMode.ScaleToFit,true);
        GUI.DrawTexture( new Rect(_joystickCenter.x+(joyTouch.x -touchSize) ,Screen.height-_joystickCenter.y-(joyTouch.y+touchSize),touchSize*2,touchSize*2), touchTexture,ScaleMode.ScaleToFit,true);
 }

}

Comment
Add comment · Show 2 · Share
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 meat5000 ♦ · Sep 01, 2013 at 10:55 AM 0
Share

You probably aint gonna get much response replying to ancient threads man :P

avatar image Amerging Staar · Jul 14, 2014 at 08:37 AM 0
Share

ohhh bro this is great (y) but sorry i am unable to view the values sent by joystick in 3rd person controller class :( it gives me error in ThirdPersonController.UpdateSmoothed$$anonymous$$ovementDirection (). Please help me.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

cycle joystick navigation similar to penelope. 0 Answers

Make Player face where Joystick is pointing? 6 Answers

How do you change the joystick axis boundary from a circle to a square? 1 Answer

Error when doing Penelope Tutorial. Need Help 1 Answer

Penelope joystick in C# 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