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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
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
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 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
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 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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity 5 mobile single stick control 6 Answers

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

MobileSingleStickControl is not what I expected. 0 Answers

How do I map joystick input to run method 0 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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges