• 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 Aissasa · May 02, 2016 at 02:55 PM · unity 5unity 2dmobilejoystickplayer movement

Change the position of the Joystick by tap

Greetings unity's community. I've been working on a 2D top-down game for mobile, and I added the standard assets' joystick to my project. I made all the necessary changes and it works like a charm now. I also added a roll system, when I drag the Joystick in the same direction twice, the player rolls, and that works fine too.

Now let's get to the problem I'm facing; I want that when the player taps in the restricted area of the Joystick, the joystick moves to that position. Kinda like Zenonia 6 controls if you are familiar with it.

That mechanic would allow the player implicitly to roll more easily by just tapping twice in the same direction. So any ideas for how could I add this feature to my game ?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Aissasa · May 03, 2016 at 01:52 PM

So I found a solution for this : I worked with Input.touches, get the last touch position, if it's in the restricted area of the joystick, I move the Joystick to that position, in the Began and Stationary phases, and return it to the central position if the phase is Ended.

If anyone is interested, here's the code to that part:

 if (Application.platform == RuntimePlatform.Android)
 {
       Touch[] taps = Input.touches;
        if (taps.Length > 0)
        {
             Touch lastTap = taps[taps.Length - 1];
              if (InRistrictedArea(new Vector3(lastTap.position.x, lastTap.position.y, 0)))
              {
                   if (lastTap.phase == TouchPhase.Began || lastTap.phase == TouchPhase.Stationary)
                         {
                          Vector3 newPos = new Vector3(lastTap.position.x - m_StartPos.x, lastTap.position.y - m_StartPos.y);
                             transform.position = Vector3.ClampMagnitude(newPos, MovementRange) + m_StartPos;
                             UpdateVirtualAxes(transform.position);
                         }
                         if (lastTap.phase == TouchPhase.Ended)
                         {
                             HandleRoll();
                             lastDragTime = Time.time;
                             lastDragPos = transform.position;
 
                             transform.position = m_StartPos;
                             UpdateVirtualAxes(m_StartPos);
                         }
                     }
                 }
 
             }
             else // to test on pc
             {
                 if (Input.GetMouseButtonDown(0))
                 {
                     Vector2 clickPos = Input.mousePosition;
 
                     if (InRistrictedArea(clickPos))
                     {
                         Vector3 newPos = new Vector3( clickPos.x - m_StartPos.x, clickPos.y - m_StartPos.y);
                         transform.position = Vector3.ClampMagnitude(newPos, MovementRange) + m_StartPos;
                         UpdateVirtualAxes(transform.position);
                     }
                 }
                 if (Input.GetMouseButtonUp(0))
                 {
                     HandleRoll();
                     lastDragTime = Time.time;
                     lastDragPos = transform.position;
 
                     transform.position = m_StartPos;
                     UpdateVirtualAxes(m_StartPos);
                 }
             }
         }

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
avatar image

Answer by InfiniteSoftware · Jul 12, 2016 at 08:06 AM

@Aissasa As I understand it, you adapted this to the Joystick Script found in the standard assets. I have a similar case. I used standard assets Joystick and my player is moving as I want him to, but now I want to add the same effect as you. But I don't get how you adapted this piece of code to the rest. Could you show us your whole code? or provide the new variables that you used, etc. Thanks in advance!

Comment

People who like this

0 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 Aissasa · Jul 12, 2016 at 01:37 PM 0
Share

All the changes are made in the Joystick class. I'll post the final code of that class. All the changes I made are surrounded by a //abenz comment. I hope that helps :D If you don't get some methods tell and I'll explain as much as I can, It's been a while. Here's a link to the code: http://pastie.org/private/8eux7wwjwdwuptcmfvw1g

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity Mobile Resolution Problems 2 Answers

Help me with mobile flight controls with joystick ? movement up and down and left and right . And the forward direction should be with given speed. 0 Answers

Unity 5 mobile single stick control 6 Answers

Show Joystick on the left part of the screen only 1 Answer

On-Screen Joystick Stutter/Lag [Unity Input System Bug] 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