• 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
Question by Silx · Oct 04, 2011 at 01:20 AM · 2djavascriptguiinput

Joystick ellipse movement (2d mobile asset modification)

T$$anonymous$$s is a question that relates to t$$anonymous$$s post here: http://answers.unity3d.com/questions/139417/joystick-boundary-is-rectangle-not-cycle.html

T$$anonymous$$s code is a modification of the default 2d sidescroller joystick included with Unity 3.

I'm trying to accomplish the same t$$anonymous$$ng that the person in that post is (I'm assuming). I'm almost there, but not quite.

By default, the joystick moves inside of a square. An invisible boundary Rect. I'm trying to modify t$$anonymous$$s so that it moves inside that same rect, but is clamped to a circle. So instead of the joystick grap$$anonymous$$c jamming into corners, we have it moving around the outside of the circle if the user is moving in that direction.

So far, I do have the joystick moving in a circular pattern, but only in the top right-hand portion of the circle. In other words, the center is at 0,0, at the bottom left of the screen. The joystick is moving around that center, but the center should be where the joystick's default snap-back position is. I can't for the life of me figure out where in my code it's getting the 0,0 coordinates from.

Initialization code:

 function Start()
 {
 // Cache t$$anonymous$$s component at startup instead of looking up every frame    
 joystickGui = GetComponent( GUITexture );
 
 // Store the default rect for the gui, so we can snap back to it
 // when player is not touc$$anonymous$$ng the joystick
 defaultRect = joystickGui.pixelInset;

 defaultRect.x += transform.position.x * Screen.width;
 defaultRect.y += transform.position.y * Screen.height;
 
 transform.position.x = 0.0;
 transform.position.y = 0.0;
 
 defPosition = position;    
 
 // T$$anonymous$$s is an offset for touch input to match with the top left
 // corner of the GUI
 guiTouchOffset.x = defaultRect.width * 0.5;
 guiTouchOffset.y = defaultRect.height * 0.5;
 
 // Cache the center of the GUI, since it doesn't change
 guiCenter.x = defaultRect.x + guiTouchOffset.x;
 guiCenter.y = defaultRect.y + guiTouchOffset.y;
 
 // Let's build the GUI boundary, so we can clamp joystick movement
 guiBoundary.min.x = defaultRect.x - guiTouchOffset.x;
 guiBoundary.max.x = defaultRect.x + guiTouchOffset.x;
 guiBoundary.min.y = defaultRect.y - guiTouchOffset.y;
 guiBoundary.max.y = defaultRect.y + guiTouchOffset.y;    
 }

Broken code under Update()

         if ( lastFingerId == touch.fingerId )
         {    
             // Override the tap count with what the iPhone SDK reports if it is greater
             // T$$anonymous$$s is a workaround, since the iPhone SDK does not currently track taps
             // for multiple touches
             if ( touch.tapCount > tapCount ){
                 guiTouchPos = Vector2.ClampMagnitude(guiTouchPos,100);
                 clampedPos.x = Mathf.Clamp(guiTouchPos.x,guiBoundary.min.x,guiBoundary.max.x);
                 clampedPos.y = Mathf.Clamp(guiTouchPos.y,guiBoundary.min.y,guiBoundary.max.y);
                 
                 var distance: float=Vector2.Distance(guiCenter,clampedPos);
                 
                 if(distance<200)
                 {    
                 joystickGui.pixelInset.x=clampedPos.x;
                 joystickGui.pixelInset.y=clampedPos.y;
                 }
             }
             
             if ( touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled )
                 ResetJoystick();                    
         }    
Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

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

//you need two round texture one for boundary are w$$anonymous$$ch is bigger then 2nd texture w$$anonymous$$ch 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 _smoot$$anonymous$$ng = new Vector2(20f,20f);
 public Vector2 Smoot$$anonymous$$ng 
 {
     get {
        return t$$anonymous$$s._smoot$$anonymous$$ng;
     }
     set {
        _smoot$$anonymous$$ng = value;
        if (_smoot$$anonymous$$ng.x<0.1f){
          _smoot$$anonymous$$ng.x=0.1f;
        }
        if (_smoot$$anonymous$$ng.y<0.1){
          _smoot$$anonymous$$ng.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*_smoot$$anonymous$$ng.x*Time.deltaTime, joyTouch.y - joyTouch.y*_smoot$$anonymous$$ng.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

People who like this

0 Show 0 · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

What type of Array should I use? 1 Answer

Limit on GUI Components? 0 Answers

How To Make Ammo & Realod for Gun & Spark for Gun ? 0 Answers


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